500. Keyboard Row
EasyView on LeetCode
Time: O(n*k)
Space: O(1)
Advertisement
Intuition
Check if all characters of a word are on the same keyboard row.
Algorithm
- 1Map each letter to its row (0,1,2): row1="qwertyuiop", row2="asdfghjkl", row3="zxcvbnm".
- 2For each word, get the row of its first character. Check all other chars are on the same row.
Common Pitfalls
- •Convert to lowercase before checking.
500.cs
C#
// Approach: Map each letter to its keyboard row (1/2/3); a word is valid
// if every character shares the same row as the first character.
// Time: O(n*k) Space: O(1)
public class Solution
{
public string[] FindWords(string[] words)
{
List<string> ans = new List<string>();
int[] rows = {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};
foreach (var word in words)
{
string lowerWord = word.ToLower();
int row = rows[lowerWord[0] - 'a'];
bool isValid = lowerWord.All(c => rows[c - 'a'] == row);
if (isValid)
ans.Add(word);
}
return ans.ToArray();
}
}Advertisement
Was this solution helpful?