My Simple C# Solution for this Easy Problem


  • 2
    Y
    public string[] FindWords(string[] words) {
            
            HashSet<char> row1 = new HashSet<char>(){'q','w','e','r','t','y','u','i','o','p'};
            HashSet<char> row2 = new HashSet<char>(){'a','s','d','f','g','h','j','k','l'};
            HashSet<char> row3 = new HashSet<char>(){'z','x','c','v','b','n','m'};
            List<string> output = new List<string>();
            
            foreach(string word in words)
            {
                string wordLower = word.ToLower();
                if(IsPresentInRow(wordLower,row1) || IsPresentInRow(wordLower,row2) || IsPresentInRow(wordLower,row3))
                {
                   output.Add(word); 
                }
            }
            return output.ToArray();
        }
        
        private bool IsPresentInRow(string input, HashSet<char> currentRow)
        {
            if(string.IsNullOrEmpty(input))
                return false;
            
            for(int i =0;i<input.Length;i++)
            {
                if(!currentRow.Contains(input[i]))
                    return false;
            }
            return true;
        }
    

Log in to reply
 

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