The question is as follows: A pangram is a string that contains every letter of the alphabet. Given a sentence determine whether it is a pangram in the English alphabet. Ignore case. Return either pangram or not pangram as appropriate.
My code works for all input except "qmExzBIJmdELxyOFWv LOCmefk TwPhargKSPEqSxzveiun", for which it returns "not pangram", even though the correct answer is "pangram." Does anybody have any ideas as to why my code is outputting the incorrect solution?
public static String pangrams(String s) {
Hashtable<Character, Integer> alpha = new Hashtable<Character, Integer>();
s = s.toLowerCase();
for (int i = 0; i < s.length()-1; i++){
if (alpha.get(s.charAt(i)) != null){
int value = alpha.get(s.charAt(i));
alpha.put(s.charAt(i), value + 1);
}
else{
alpha.put(s.charAt(i), 1);
}
}
if (alpha.size() != 27){
return "not pangram";
}
else{
return "pangram";
}
}
i < s.length() - 1
, so you are missing the last character, the "n". The correct loop condition would bei < s.length()
. As a sidenote, why is the whitespace considered as a letter of the alphabet and not filtered out?Set<Character>
for example.alpha.size() != 26
instead of27
. As per the logic of the program, you might also want to do something likeif(Character.isLetter(s.charAt(i)))
before adding. And also if you are only interested in the pangram then I don't think you need to add one to the value unless you want to count the occurences, hence you could simply do justalpha.put(s.charAt(i), 1)