Check if string has all the letters of the alphabet

前端 未结 15 1145
终归单人心
终归单人心 2020-12-19 17:51

What would be the best logic to check all the letters in a given string.

If all the 26 letters are available in the provided string, I want to check that and perform

相关标签:
15条回答
  • 2020-12-19 18:28

    try this method, is very simple and works fast

    public static String checkSentence(String mySentence)
    {
        String result = "";
        char[] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    
        String missingLetters = "";
        int countMissLetters = 0;
    
        for (int i = 0; i < alphabet.length; i++)
        {
            if (!mySentence.toLowerCase().contains(String.valueOf(alphabet[i]).toLowerCase()))
                {
                    missingLetters = missingLetters + String.valueOf(alphabet[i]) + " ";
                    countMissLetters++;
                }
        }
    
        result = "Letters who are missing: " + missingLetters + "Counted: " + countMissLetters;
        return result;
    }
    

    with this exemple

    System.out.println(checkSentence("Your own sentence to test."));
    System.out.println(checkSentence("abcdefghijklmnoprstuvwxyz"));
    

    will return this results

    Letters who are missing: a b d f g h i j k l m p q v x z Counted: 16
    Letters who are missing: q Counted: 1
    
    0 讨论(0)
  • Just another solution, using Array instead of Set

    public static boolean isPanagram(String input) {
        if(input == null || input.trim().equals(""))
            return false;
    
        boolean[] chars = new boolean[26];
        int count = 0;
        for(char c : input.toLowerCase().toCharArray()) {
            if(Character.isAlphabetic(c)) {
                int val = (int) c - 'a';
                if(val >= 0 || val <= 26) {
                    if(!chars[val]) {
                        count++;
                        chars[val] = true;
                    }
                }
                // checking if all 26 alphabets are set, for every new alphabet found. 
                // so can return true immediately instead of processing entire string
                if(count==26)
                    return true;
            }
        }
    
        return false;
    }
    
    0 讨论(0)
  • 2020-12-19 18:30

    Try this one out it's easy and simple to understand

    import java.util.*;
    
    public class Pnagram{
        public static void main(String[] args){
            Scanner sc=new Scanner(System.in);
            String Str=sc.nextLine();
            Set<Character> set = new HashSet<Character>();
    
            for(char c:Str.toUpperCase().toCharArray()){
                if(Character.isLetter(c))
                set.add(c);
            }
            if(set.size()==26)
                System.out.println("pnagram");
            else
                System.out.println("not pnagram");
        }
    }
    
    0 讨论(0)
提交回复
热议问题