Easy to understand Java Solution


  • 0
    E
    public class Solution {
        public String[] findWords(String[] words) {
            String str1="QWERTYUIOPqwertyuiop";
            String str2="ASDFGHJKLasdfghjkl";
            String str3="ZXCVBNMzxcvbnm";
            ArrayList<String>list=new ArrayList<String>();
            for(int i=0;i<words.length;i++){
                String ref="";
                if(str1.contains(words[i].charAt(0)+"")){
                    ref=str1;
                }
                else if(str2.contains(words[i].charAt(0)+"")){
                    ref=str2;
                }
                else{
                    ref=str3;
                }
                int flag=0;
                for(int j=0;j<words[i].length();j++){
                    if(!ref.contains(words[i].charAt(j)+"")){
                        flag=1;
                        break;
                    }
                }
                if(flag!=1){
                    list.add(words[i]);
                }
            }
            String res[]=new String[list.size()];
            for(int j=0;j<list.size();j++){
                res[j]=list.get(j);
            }
            return res;
        }
    }
    

Log in to reply
 

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