Java Using String/Character Methods


  • 0
    B
    public class Solution {
       public String[] findWords(String[] words) {
            String row1 = "qwertyuiop";
            String row2 = "asdfghjkl";
            String row3 = "zxcvbnm";
            List<String> returnList = new ArrayList<String>();
            for (int i = 0; i < words.length; i++){
                String holder = words[i];
                Boolean add = false;
                int counter1 = 0;
                int counter2 = 0;
                int counter3 = 0;
                for (int j = 0; j < holder.length(); j++){
                    if (row1.contains(String.valueOf(Character.toLowerCase(holder.charAt(j)))))
                        counter1++;
                    if (row2.contains(String.valueOf(Character.toLowerCase(holder.charAt(j)))))
                        counter2++;
                    if (row3.contains(String.valueOf(Character.toLowerCase(holder.charAt(j)))))
                        counter3++;
                }
                if (counter1 == holder.length() || counter2 == holder.length() || counter3 == holder.length())
                    returnList.add(holder);
            }
            String[] returnWords = new String[returnList.size()];
            for (int j = 0; j < returnList.size(); j++){
                returnWords[j] = returnList.get(j);
            }
            return returnWords;
        }
    }
    

Log in to reply
 

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