2021.04.19 연습

 

  • 사용언어 : java

 


class Solution {
    public boolean checkIfPangram(String sentence) {
        
        if (sentence.length() < 26) {
            return false;
        }
        
        Set<Character> set = new HashSet<Character>();
        
        for (char i : sentence.toCharArray()) {
            
            if (!set.contains(i)) {
                set.add(i);
            }
            
        }
        
        if (set.size() == 26) {
            return true;
        } else {
            return false;
        }
        
    }
}

Runtime: 2 ms, faster than 25.00% of Java online submissions for Check if the Sentence Is Pangram. Memory Usage: 37.2 MB, less than 25.00% of Java online submissions for Check if the Sentence Is Pangram.

 

  • 다른 코드 참고
//Idea is to make a 26 size array (representing a to z ) and mark the presence of character in it.
class Solution {
    public boolean checkIfPangram(String sentence) {
        boolean[] letters  = new boolean[26];
        
        for(char c : sentence.toCharArray()) {
            letters[c - 'a'] = true;
        }
        
        //find any letter that not exist
        for(boolean existLetter : letters) {
            if(!existLetter) return false;
        }
        
        return true;
    }
}