Check if given String is Pangram or not in JavaGiven string str, our task is to write a Java program that determines if the provided string is a pangram or not. If all of the alphabetic characters are present in a string without considering its case (Uppercase or lowercase), the string is called a pangram string. Example 1: Input: String str = "abCDEfghIJKlmnoPQRStuvwxyZ" Output: The given string is a Pangram. Explanation: For the given string "abCDEfghIJKlmnoPQRStuvwxyZ", all the 26 alphabets are present without the regard for the case-sensitivity feature. Hence the given string is a Pangram. Example 2: Input: String str = "The five boxing wizards jump quickly." Output: The given string is a Pangram. Explanation: For the given string, "The five boxing wizards jump quickly," since all 26 alphabets are present without the regard for the case-sensitivity feature. Hence the given string is a Pangram. Example 3: Input: String str = "Hello Welcome to the World" Output: The given string is not a Pangram. Explanation: For the given string "Hello Welcome to the World", since all the 26 alphabets are not present. Hence the given string is not a Pangram. Approach: Using Set The use of a set makes sense since, in order to verify a pangram, all 26 alphabets must be present, irrespective of whether the character is uppercase or lowercase. The alphabets have been placed in lowercase, and the set size needs to be 26 in order to determine whether an alphabet is already present or not. Algorithm: Step 1: Initialize a character set. Step 2: Check the entire string and make certain every character is there. Step 3: Insert into the set if the character is a lowercase alphabet. Step 4: Use the tolower() function to change any capital letters to lowercase before inserting them. Step 5: At the conclusion, verify that the set size is 26. If accurate, this indicates that every alphabet, whether lowercase or uppercase, was used in the string. Implementation:FileName: ApproachSetPangram.java Output The given string is a Pangram. Complexity Analysis: The Time Complexity of the above code is O(N*logN), where 'N' represents the length of the string and the Space Complexity is O(1). Approach: Using Hashing TechniqueMake a mark[] array of Boolean types, iterate over each character in the string, and mark each one as visited. Case and capital letters are regarded as interchangeable. Thus, index 0 is used to indicate "A" and "a," and index 25 is used to mark "Z" and "z." Once every character has been iterated through, determine whether or not every character has been marked. If not, return false because there is no pangram here; otherwise, return true. Algorithm: Step 1: Initially, make a bool vector mark[] with a size of 26. Step 2: Loop over each character in the string s, marking s[i] - "a" or s[i] - "A" as 1 for the upper and lower case, respectively. Step 3: Go through each of mark's indices once. Step 3.1: A pangram is returned if every index is marked as visited. Step 3.2: There is no Pangram in the Else returns. Implementation:FileName: Output The given string is a Pangram. Complexity Analysis: The Time Complexity is O(N), where N represents the length of the given string, and the Space Complexity is O(1). Approach: Using Frequency ArrayAlgorithm: Step 1: Convert all of the string's letters to upper or lower case. Step 2: To indicate the frequency of each letter from a to z, create a frequency array. Step 3: Next, iterate through the frequency array and print "No" if any letter is absent from the supplied string and "Yes" otherwise. Implementation:FileName: reqarrayApproachPangram.java Output The given string is a Pangram. Complexity Analysis: The Time Complexity is O(N), where N represents the length of the given string, and the Space Complexity is O(1). Approach: Using TraversalAlgorithm: Step 1: Declare the LettersPresent flag's initial value to true. Step 2: Repeat steps a through z for every letter. Step 2.1: Determine whether the letter is in the string. Step 3: Break the loop and set the flag to false if any letters are missing. Step 4: Print "The given string is a Pangram" if every letter is present. Step 5: Else, print that "The given string is not a Pangram." Implementation:FileName: TraversalApproachPangram.java Output The given string is a Pangram. Complexity Analysis: The Time Complexity is O(26*N), where N represents the length of the given string, and the Space Complexity is O(1). |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India