Java accepted solution, Please comment and suggest


  • 1
    J

    Please comment and suggest

    public class Solution {
        public String[] findWords(String[] words) {
            List<String> list = new ArrayList<String>();
            String row1 = "qwertyuiop";
            String row2 = "asdfghjkl";
            String row3 = "zxcvbnm";
            
            for(String temp : words){
                if(checkValidWordWithRow(temp.toLowerCase(), row1))
                    list.add(temp);
                if(checkValidWordWithRow(temp.toLowerCase(), row2))
                    list.add(temp);
                if(checkValidWordWithRow(temp.toLowerCase(), row3))
                    list.add(temp);
        }
            return list.toArray(new String[list.size()]);
    }
        
        private boolean checkValidWordWithRow(String word, String row){
            for(int i=0; i<word.length();i++){
                if(row.indexOf(word.charAt(i)+"") < 0) 
                    return false;
            }
            return true;
        }
    }
    

  • 0
    G

    Why have you added

    ""
    

    in

    row.indexOf(word.charAt(i)+"")
    

    Can you not do it without the "" ?


Log in to reply
 

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