Question:
How to check whether a string is Panagram or not using Java

Summary

Let’s assume:

Function- checkPangram


This function will help you check whether the given string is a pangram or not. 


Solution

//User function template for JAVA


class Solution

{

    //Function to check if a string is Pangram or not.

    public static boolean checkPangram  (String s) {

         boolean[] present = new boolean[26];


        // Convert the string to lowercase for case-insensitive comparison

        s = s.toLowerCase();


        // Iterate through the string and mark each letter as present

        for (int i = 0; i < s.length(); i++) {

            char currentChar = s.charAt(i);


            // Check if the character is a lowercase letter

            if (currentChar >= 'a' && currentChar <= 'z') {

                present[currentChar - 'a'] = true;

            }

        }


        // Check if all letters are present

        for (boolean letterPresent : present) {

            if (!letterPresent) {

                return false;

            }

        }


        return true;

        // your code here

    }

}


Explanation

  • To indicate whether a letter in the alphabet is present, use the boolean[] present array. There is one for every lowercase letter in its 26 elements.

  • To make the comparison case-insensitive, s.toLowerCase() is used to convert the input string s to lowercase.

  • Each character in the string is iterated through in a loop.


Answered by:> >imranwadc7b

Credit:> >GeekforGeeks


Suggested blogs:

>How to detect whether the linked list has a loop using Java

>How to determine the number of valid groupings for a string using Java

>How to mock a constant value in Python?

>Creating a pivot table by 6 month interval rather than year

>How to Install mariaDB client efficiently inside docker?

>Can I call an API to find out the random seed in Python `hash()` function?

>How remove residual lines after merge two regions using Geopandas in python?


Submit
0 Answers