地址 https://leetcode-cn.com/problems/keyboard-row/
给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词。 示例: 输入: ["Hello", "Alaska", "Dad", "Peace"] 输出: ["Alaska", "Dad"] 注意: 你可以重复使用键盘上同一字符。 你可以假设输入的字符串将只包含字母。
算法1
哈希 做好字母和字母在键盘上的行数的映射即可
注意大小写处理 要么哈希包含全部大小写
要么处理的时候把单词转化为全部一致的大小写
class Solution { public: map<char, int> mm{ {'q',0},{'w',0},{'e',0},{'r',0},{'t',0},{'y',0},{'u',0},{'i',0},{'o',0},{'p',0},{'a',1},{'s',1},{'d',1}, {'f',1},{'g',1},{'h',1},{'j',1},{'k',1},{'l',1},{'z',2},{'x',2},{'c',2},{'v',2},{'b',2},{'n',2},{'m',2}, {'Q',0},{'W',0},{'E',0},{'R',0},{'T',0},{'Y',0},{'U',0},{'I',0},{'O',0},{'P',0},{'A',1},{'S',1},{'D',1}, {'F',1},{'G',1},{'H',1},{'J',1},{'K',1},{'L',1},{'Z',2},{'X',2},{'C',2},{'V',2},{'B',2},{'N',2},{'M',2} }; vector<string> findWords(vector<string>& words) { vector<string> ans; if (words.empty()) return ans; for (auto& s : words) { int k = mm[s[0]]; int find = 1; for (int i = 0; i < s.size(); i++) { if (mm[s[i]] != k) { find = 0; break; } } if (find == 1) { ans.push_back(s); } } return ans; } };