Loading
Leetcode / 500. Keyboard Row

Pick a programming language:

Here is the source code for the solution to this problem.

class Solution {
    public String[] findWords(String[] words) {
        HashMap<Character, Integer> hashMap = new HashMap<Character, Integer>();

        hashMap.put('q', 1);
        hashMap.put('w', 1);
        hashMap.put('e', 1);
        hashMap.put('r', 1);
        hashMap.put('t', 1);
        hashMap.put('y', 1);
        hashMap.put('u', 1);
        hashMap.put('i', 1);
        hashMap.put('o', 1);
        hashMap.put('p', 1);

        hashMap.put('a', 2);
        hashMap.put('s', 2);
        hashMap.put('d', 2);
        hashMap.put('f', 2);
        hashMap.put('g', 2);
        hashMap.put('h', 2);
        hashMap.put('j', 2);
        hashMap.put('k', 2);
        hashMap.put('l', 2);

        hashMap.put('z', 3);
        hashMap.put('x', 3);
        hashMap.put('c', 3);
        hashMap.put('v', 3);
        hashMap.put('b', 3);
        hashMap.put('n', 3);
        hashMap.put('m', 3);

        List<String> list = new LinkedList<String>();

        outerloop:
        for (int i = 0; i < words.length; i++) {
            HashSet<Integer> hashSet = new HashSet<Integer>();
            for (int j = 0; j < words[i].length(); j++) {
                char c = Character.toLowerCase(words[i].charAt(j));

                hashSet.add(hashMap.get(c));

                if (hashSet.size() > 1) {
                    continue outerloop;
                }
            }
            list.add(words[i]);
        }

        return list.toArray(String[]::new);
    }

    // alternative solution using sets
    // class Solution {
    //     public String[] findWords(String[] words) {
    //         HashSet<Character> hashSet1 = new HashSet<Character>();
    //         HashSet<Character> hashSet2 = new HashSet<Character>();
    //         HashSet<Character> hashSet3 = new HashSet<Character>();
    
    //         hashSet1.add('q');
    //         hashSet1.add('w');
    //         hashSet1.add('e');
    //         hashSet1.add('r');
    //         hashSet1.add('t');
    //         hashSet1.add('y');
    //         hashSet1.add('u');
    //         hashSet1.add('i');
    //         hashSet1.add('o');
    //         hashSet1.add('p');
    
    //         hashSet2.add('a');
    //         hashSet2.add('s');
    //         hashSet2.add('d');
    //         hashSet2.add('f');
    //         hashSet2.add('g');
    //         hashSet2.add('h');
    //         hashSet2.add('j');
    //         hashSet2.add('k');
    //         hashSet2.add('l');
    
    //         hashSet3.add('z');
    //         hashSet3.add('x');
    //         hashSet3.add('c');
    //         hashSet3.add('v');
    //         hashSet3.add('b');
    //         hashSet3.add('n');
    //         hashSet3.add('m');
    
    //         List<String> list = new LinkedList<String>();
    
    //         outerloop:
    //         for (int i = 0; i < words.length; i++) {
    //             HashSet<Character> hashSet = new HashSet<Character>();
    
    //             for (int j = 0; j < words[i].length(); j++) {
    //                 char c = Character.toLowerCase(words[i].charAt(j));
    
    //                 hashSet.add(c);
    //             }
    
    //             if (hashSet1.containsAll(hashSet) || hashSet2.containsAll(hashSet) || hashSet3.containsAll(hashSet)) {
    //                 list.add(words[i]);
    //             }
    //         }
    
    //         return list.toArray(String[]::new);
    //     }
    // }
}
Did you like the lesson? 😆👍
Consider a donation to support our work: