C# solution without map and regex


  • 0
    N
    static int[] rowNumbers = new int[] {2,3,3,2,1,2,2,2,1,2,2,2,3,3,1,1,1,1,2,1,1,3,1,3,1,3};
        public string[] FindWords(string[] words) { 
            List<string> InARow = new List<string>();
            for(int i =0; i < words.Length; i++)
            {
                if(AreLettersInARow(words[i]))
                {
                    InARow.Add(words[i]);
                }
            }
            return InARow.ToArray();
        }
        public bool AreLettersInARow(string word){
           int lookup = 0, counter =0;
            word = word.ToLower();
            counter = rowNumbers[(int)word[0] - (int)'a'];
            for(int i =1; i< word.Length; i++)
            {
                lookup = rowNumbers[(int)word[i] - (int)'a'];
                if( lookup != counter)
                {
                    return false;
                }
            }       
            return true;        
        }
    

Log in to reply
 

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