Skip to main content
  1. About
  2. For Teams
Asked
Viewed 138 times
0

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";
    }
        
}
4
  • Your loop condition is incorrect. Currently it is i < s.length() - 1, so you are missing the last character, the "n". The correct loop condition would be i < s.length(). As a sidenote, why is the whitespace considered as a letter of the alphabet and not filtered out?
    maloomeister
    –  maloomeister
    2021-08-12 07:42:54 +00:00
    Commented Aug 12, 2021 at 7:42
  • However, currently special characters like "." in the input string would break your code and get you the incorrect output. So you should add some logic to determine if a character is actually a letter of the alphabet. Also, if you don't actually need the count of occurrence of each individual character (which it seems like you don't), you could simplify your code by simply using a Set<Character> for example.
    maloomeister
    –  maloomeister
    2021-08-12 07:46:56 +00:00
    Commented Aug 12, 2021 at 7:46
  • I think you want to check alpha.size() != 26 instead of 27. As per the logic of the program, you might also want to do something like if(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 just alpha.put(s.charAt(i), 1)
    Francesco
    –  Francesco
    2021-08-12 09:00:18 +00:00
    Commented Aug 12, 2021 at 9:00
  • maloomeister, your suggestion worked! I changed the for loop to I <s.length() and got the correct output. Addressing your follow-up question, because I am iterating through the entire string and have not filtered out for whitespace, the hashtable picks up the whitespace as a separate key!
    sanjna_007
    –  sanjna_007
    2021-08-12 09:43:26 +00:00
    Commented Aug 12, 2021 at 9:43

1 Answer 1

0

As per your definition of pangram you might want to consider the following:

  1. Use a Set instead of a Map
  2. As far as I know, English has 26 letters
  3. Make sure you don't mistakenly add digits or symbols to your set

Given the three above statements I would slightly change your code to reflect that. That would, more or less, look something like this:

public static String pangrams(String s) {
    Set<Character> alpha = new HashSet<>();
    s = s.toLowerCase();
    for (int i = 0; i < s.length(); i++){
        char c = s.charAt(i);
        if (Character.isLetter(c)){
            alpha.add(c);
        }
    }
    if (alpha.size() == 26){
        return "pangram";
    }
    return "not pangram";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.