Why my java code return null array?


  • 0
    N

    public class Solution {
    public String[] findWords(String[] words) {
    HashMap<String, String> hm = new HashMap<String, String>();
    HashSet<Character> hs1 = new HashSet<Character>();
    hs1.add('q');
    hs1.add('w');
    hs1.add('e');
    hs1.add('r');
    hs1.add('t');
    hs1.add('y');
    hs1.add('u');
    hs1.add('i');
    hs1.add('o');
    hs1.add('p');

        HashSet<Character> hs2 = new HashSet<Character>();
        hs2.add('a');
        hs2.add('s');
        hs2.add('d');
        hs2.add('f');
        hs2.add('g');
        hs2.add('h');
        hs2.add('j');
        hs2.add('k');
        hs2.add('l');
        
        HashSet<Character> hs3 = new HashSet<Character>();
        hs3.add('z');
        hs3.add('x');
        hs3.add('c');
        hs3.add('v');
        hs3.add('b');
        hs3.add('n');
        hs3.add('m');
        
        for(int i = 0; i < words.length; i++)
        {
            String originalWords = words[i];
            words[i].toLowerCase();
            HashSet<Character> record = new HashSet<Character>();
            
            //check hs1
            if(hs1.contains(words[i].charAt(0)))
            {
                record.add('y');
                for(int j = 1; j < words[i].length(); j++)
                {
                    if(!hs1.contains(words[i].charAt(j)))
                    {
                        record.add('n');
                    }
                    else
                    {
                        record.add('y');
                    }
                }
                
                if(!record.contains('n'))
                {
                    hm.put(originalWords, "true");
                }
                else
                {
                    hm.put(originalWords, "false");
                }
            }
            
            // check hs2
            if(hs2.contains(words[i].charAt(0)))
            {
                record.add('y');
                for(int j = 1; j < words[i].length(); j++)
                {
                    if(!hs2.contains(words[i].charAt(j)))
                    {
                        record.add('n');
                    }
                    else
                    {
                        record.add('y');
                    }
                }
                
                if(!record.contains('n'))
                {
                    hm.put(originalWords, "true");
                }
                else
                {
                    hm.put(originalWords, "false");
                }
            }
            
            //check hs3
            if(hs3.contains(words[i].charAt(0)))
            {
                record.add('y');
                for(int j = 1; j < words[i].length(); j++)
                {
                    if(!hs3.contains(words[i].charAt(j)))
                    {
                        record.add('n');
                    }
                    else
                    {
                        record.add('y');
                    }
                }
                
                if(!record.contains('n'))
                {
                    hm.put(originalWords, "true");
                }
                else
                {
                    hm.put(originalWords, "false");
                }
            }
        }
        
       ArrayList<String> result = new ArrayList<String>();
       for(String s : hm.keySet())
       {
           if(hm.get(s).equals("true"))
           {
               result.add(s);
           }
       }
       return result.toArray(new String[result.size()]);
    }
    

    }


  • 0
    2

    @NewStartToLeetcode

    probably easier to read if the code is wrapped in the ```


Log in to reply
 

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