Share a very Easy and Simple Java Solution


  • 0
    L
    public class Solution {
        public String[] findWords(String[] words) {
            Set<Character> Row1 = new HashSet<Character>(Arrays.asList('q','w','e','r','t','y','u','i','o','p'));
            Set<Character> Row2 = new HashSet<Character>(Arrays.asList('a','s','d','f','g','h','j','k','l'));
            Set<Character> Row3 = new HashSet<Character>(Arrays.asList('z','x','c','v','b','n','m'));
            ArrayList<String> ret = new ArrayList<String>();
            for (int i = 0; i < words.length; i++) {
                char[] word = words[i].toLowerCase().toCharArray();
                int count = word.length;
                int row1count = 0;
                int row2count = 0;
                int row3count = 0;
                for (char ch: word) {
                    if (Row1.contains(ch)){
                        row1count++; 
                    }
                    if (Row2.contains(ch)){
                        row2count++;
                    }
                    if (Row3.contains(ch)){
                        row3count++;
                    }
                }
                if(row1count == count||row2count== count||row3count == count) { ret.add(words[i]);}
            }
            return ret.toArray(new String[ret.size()]);
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.