patternjavaMinor
Panagram solution for a given string
Viewed 0 times
panagramgivenforsolutionstring
Problem
I want to check if the given string is a Panagram or not.
A Panagram is a sentence containing every letter of the alphabet at least once.
This is what I've come up with as solution:
Any review comments or suggestions?
List of well known Panagrams.
A Panagram is a sentence containing every letter of the alphabet at least once.
This is what I've come up with as solution:
import java.util.Scanner;
public class Panagram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String inputString = "";
System.out.print("Enter the input: ");
inputString = sc.nextLine();
sc.close();
if (inputString != null && inputString.length() > 0) {
System.out.print("The given string, \"" + inputString + "\", is " + (isPanagram(inputString) ? "" : ("not ")) + "a Panagram");
} else {
System.out.print("Not a valid string!");
}
}
/**
* Checks if the given input is a panagram.
*
* @param inputString
* @return boolean
*/
private static boolean isPanagram(String inputString) {
if (inputString.length() < 26) {
return false;
}
inputString = inputString.toLowerCase();
for (char i = 'a'; i <= 'z'; i++) {
if (!(inputString.contains(i + ""))) {
return false;
}
}
return true;
}
}Any review comments or suggestions?
List of well known Panagrams.
Solution
Your solution is actually quite decent, and the code style is good, etc. There are a couple of style changes I would recommend:
-
In a for loop, the variable
Now, about the algorithm, your code will scan the result as many as 26 times. In reality, this will happen fast, and there's no pressing need to change that, but it could be faster, but would require a little work to get it right.
If you reverse the logic, and just iterate through each characters in the String once, then you can reference it back to an index for the characters.... It would go something like:
Now, for long strings, the above will be more efficient than your solution. It goes through each character in the string just once, but it has to create a list of what's been seen, or not. If it is a new character, it has to count it as seen.
Your mileage may vary.
-
In a for loop, the variable
i typically indicates an int, and people tend to use c to indicate a char. So, I would write for (char c = 'a'; c
-
your isPanagram` method should be public.Now, about the algorithm, your code will scan the result as many as 26 times. In reality, this will happen fast, and there's no pressing need to change that, but it could be faster, but would require a little work to get it right.
If you reverse the logic, and just iterate through each characters in the String once, then you can reference it back to an index for the characters.... It would go something like:
boolean[] seen = new boolean[26]; // or a constant for the number of characters.
int stillNeed = seen.length;
for (int i = 0; i = 'a' && c <= 'z') {
c = c - 'a';
if (!seen[c]) {
seen[c] = true;
stillNeed--;
if (stillNeed == 0) {
return true;
}
}
}
}
return false;Now, for long strings, the above will be more efficient than your solution. It goes through each character in the string just once, but it has to create a list of what's been seen, or not. If it is a new character, it has to count it as seen.
Your mileage may vary.
Code Snippets
boolean[] seen = new boolean[26]; // or a constant for the number of characters.
int stillNeed = seen.length;
for (int i = 0; i < inputString.length(); i++) {
char c = Character.toLowerCase(inputString.charAt(i));
if (c >= 'a' && c <= 'z') {
c = c - 'a';
if (!seen[c]) {
seen[c] = true;
stillNeed--;
if (stillNeed == 0) {
return true;
}
}
}
}
return false;Context
StackExchange Code Review Q#63648, answer score: 7
Revisions (0)
No revisions yet.