Given an array of strings words, return the words that can be typed using letters of the alphabet on only one row of American keyboard like the image below.
Note that the strings are case-insensitive , both lowercased and uppercased of the same letter are treated as if they are at the same row.
In the American keyboard :
the first row consists of the characters "qwertyuiop",
the second row consists of the characters "asdfghjkl", and
the third row consists of the characters "zxcvbnm".
Input: words =["Hello","Alaska","Dad","Peace"]Output: ["Alaska","Dad"]Explanation:
Both `"a"` and `"A"` are in the 2nd row of the American keyboard due to caseinsensitivity.
Each letter belongs to a specific keyboard row. If all letters of a word belong to the same row, the word is valid. We can map each letter to its row and check each word.
classSolution {
public: vector<string> findWords(vector<string>& words) {
vector<string> rows = {"qwertyuiop", "asdfghjkl", "zxcvbnm"};
unordered_map<char, int> mp;
for (int i =0; i <3; ++i)
for (char c : rows[i]) mp[c] = i, mp[toupper(c)] = i;
vector<string> ans;
for (auto& w : words) {
int row = mp[w[0]];
bool ok = true;
for (char c : w) if (mp[c] != row) { ok = false; break; }
if (ok) ans.push_back(w);
}
return ans;
}
};
classSolution {
public String[]findWords(String[] words) {
String[] rows = {"qwertyuiop", "asdfghjkl", "zxcvbnm"};
Map<Character, Integer> mp =new HashMap<>();
for (int i = 0; i < 3; ++i)
for (char c : rows[i].toCharArray()) {
mp.put(c, i); mp.put(Character.toUpperCase(c), i);
}
List<String> ans =new ArrayList<>();
for (String w : words) {
int row = mp.get(w.charAt(0));
boolean ok =true;
for (char c : w.toCharArray()) if (mp.get(c) != row) { ok =false; break; }
if (ok) ans.add(w);
}
return ans.toArray(new String[0]);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
funfindWords(words: Array<String>): Array<String> {
val rows = arrayOf("qwertyuiop", "asdfghjkl", "zxcvbnm")
val mp = mutableMapOf<Char, Int>()
for (i in0..2) for (c in rows[i]) { mp[c] = i; mp[c.uppercaseChar()] = i }
val ans = mutableListOf<String>()
for (w in words) {
val row = mp[w[0]]
if (w.all { mp[it] == row }) ans.add(w)
}
return ans.toTypedArray()
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution:
deffindWords(self, words: list[str]) -> list[str]:
row1 = set('qwertyuiop')
row2 = set('asdfghjkl')
row3 = set('zxcvbnm')
ans = []
for w in words:
s = set(w.lower())
if s <= row1 or s <= row2 or s <= row3:
ans.append(w)
return ans
use std::collections::HashMap;
impl Solution {
pubfnfind_words(words: Vec<String>) -> Vec<String> {
let rows = ["qwertyuiop", "asdfghjkl", "zxcvbnm"];
letmut mp = HashMap::new();
for (i, row) in rows.iter().enumerate() {
for c in row.chars() {
mp.insert(c, i);
mp.insert(c.to_ascii_uppercase(), i);
}
}
letmut ans =vec![];
'outer: for w in&words {
let row = mp[&w.chars().next().unwrap()];
for c in w.chars() {
if mp[&c] != row { continue 'outer; }
}
ans.push(w.clone());
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
findWords(words: string[]):string[] {
constrows= ["qwertyuiop", "asdfghjkl", "zxcvbnm"];
constmp=newMap<string, number>();
for (leti=0; i<3; ++i)
for (constcofrows[i]) { mp.set(c, i); mp.set(c.toUpperCase(), i); }
constans: string[] = [];
for (constwofwords) {
constrow=mp.get(w[0]);
if ([...w].every(c=>mp.get(c) ===row)) ans.push(w);
}
returnans;
}
}