share my java solution!


  • 0
    T
    
    public class Solution {
        public String[] findWords(String[] words) {
            Set<Character> set1=new HashSet<>(Arrays.asList('Q','W','E','R','T','Y','U','I','O','P'));
            Set<Character> set2=new HashSet<>(Arrays.asList('A','S','D','F','G','H','J','K','L'));
            Set<Character> set3=new HashSet<>(Arrays.asList('Z','X','C','V','B','N','M'));
            
            Map<Character,Integer> map=new HashMap<>();
            for(Character c: set1){
                map.put(c,1);
            }
            for(Character c:set2){
                map.put(c,2);
            }
            for(Character c:set3){
                map.put(c,3);
            }
            
            List<String> res=new ArrayList<>();
            for(int i=0;i<words.length;i++){
                char[] array=words[i].toCharArray();
                Integer id=null;
                boolean check=true;
                for(int j=0;j<array.length;j++){
                    char c=Character.toUpperCase(array[j]);
                    if(id==null){
                        id=map.get(c);
                    }else if(id!=map.get(c)){
                        check=false;
                        break;
                    }
                }
                
                if(check==true){
                    res.add(words[i]);
                }
            }
            
            String[] ans=new String[res.size()];
            int i=0;
            for(String str:res){
                ans[i]=res.get(i);
                i++;
            }
            return ans;
            
            
            
            
        }
    }
    

Log in to reply
 

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