Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.

Example 1:

Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]

Note:

  1. You may use one character in the keyboard more than once.
  2. You may assume the input string will only contain letters of alphabet.

Subscribe to see which companies asked this question.

#include<iostream>
#include<string>
#include<vector>
#include<map>
#include <algorithm>
using namespace std;
class Solution {
public:
void findWords(vector<string>& words) {
vector<string> strs ={ "QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM" };
map<char, int> map;
for (int i = 0; i<strs.size(); i++){
for (char c : strs[i]){
map.insert(pair<char, int>(c, i));//put <char, rowIndex> pair into the map
}
}
vector<string> res;
vector<string> dst(words.size());

for (int i = 0; i < words.size(); i++)
transform(words[i].begin(), words[i].end(), back_inserter(dst[i]), ::toupper);

for (int i = 0; i<dst.size(); i++){
if (dst[i] == "")
continue;
int index = map[dst[i][0]];
for (int j = 0;j<dst[i].size();j++){
if (map[dst[i][j]] != index){
index = -1; //don't need a boolean flag.
break;
}
}
if (index != -1) res.push_back(words[i]);//if index != -1, this is a valid string
}
for (auto w : res){
cout << w << " ";
cout << endl;
}

}
};
int main()
{
vector<string> word = { "Hello", "Alaska", "Dad", "Peace" };
Solution s;
s.findWords(word);
system("pause");
return 0;
}

500. Keyboard Row的更多相关文章

  1. 46. leetcode 500. Keyboard Row

    500. Keyboard Row Given a List of words, return the words that can be typed using letters of alphabe ...

  2. Leetcode#500. Keyboard Row(键盘行)

    题目描述 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例1: 输入: ["Hello", "Alaska", &quo ...

  3. Week4 - 500.Keyboard Row & 557.Reverse Words in a String III

    500.Keyboard Row & 557.Reverse Words in a String III 500.Keyboard Row Given a List of words, ret ...

  4. LeetCode 500 Keyboard Row 解题报告

    题目要求 Given a List of words, return the words that can be typed using letters of alphabet on only one ...

  5. 【leetcode】500. Keyboard Row

    问题描述: Given a List of words, return the words that can be typed using letters of alphabet on only on ...

  6. LeetCode: 500 Keyboard Row (easy)

    题目: Given a List of words, return the words that can be typed using letters of alphabet on only one ...

  7. LeetCode 500. Keyboard Row (键盘行)

    Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...

  8. [LeetCode] 500. Keyboard Row 键盘行

    Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...

  9. 【LeetCode】500. Keyboard Row 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解 字典 + set 日期 题目地址:https ...

随机推荐

  1. 快速排序算法javascript实现

    function quicksort(arr){ function q(start,end){ if(start>=end){return;} var pivot = start, temp = ...

  2. TCP和UDP 的区别和适用场合

    TCP和UDP式TCP/IP中能够实现传输层功能的.具有代表性的协议,其主要特点和区别如下: TCP: 面向连接的.可靠的流协议.为提供可靠性传输,TCP实行"顺序控制"或&quo ...

  3. mybatis基础,mybatis核心配置文件properties元素

    peroperties元素 可外部配置且可动态替换的,既可以在典型的 Java 属性文件中配置,亦可通过 properties 元素的子元素来传递 为dataSource元素配置 <proper ...

  4. Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动

    之前在Spring Boot启动过程(二)提到过createEmbeddedServletContainer创建了内嵌的Servlet容器,我用的是默认的Tomcat. private void cr ...

  5. SQL-ROW_NUMBER() OVER函数的基本用法(源码案例)

    SELECT SUM(t.AdjustedBalance) AS Allqmye FROM ( SELECT * FROM ( SELECT ROW_NUMBER() OVER ( PARTITION ...

  6. 使用awk截取某时间段的日志

    想要取出文件里面时间是9点到12点的数据,文件内容如下: 2012-09-05 01:48:47,150 WARN  [WorkManager(3)-72] [service.PhoneRangeMa ...

  7. 万人迷”微信小程序似乎开始掉粉 为什么呢?

    "万人迷"微信小程序最近似乎开始掉粉. 距离1月9日小程序上线已有一周,相比浓烈的讨论气氛,用户的使用热情逐步降低,而部分公司开始撤离小程序. 其中,逻辑思维旗下产品"得 ...

  8. JavaScript的基本规范

    1.不要在同一行声明多个变量: 2.请使用===/!==来比较true/false或者数值: 3.使用对象字面量替代new Array这种形式: 4.Switch语句必须带有default分支: 5. ...

  9. 菜鸟Scrum敏捷实践系列(二)用户故事验收

    菜鸟Scrum敏捷实践系列索引 菜鸟Scrum敏捷实践系列(一)用户故事概念 菜鸟Scrum敏捷实践系列(二)用户故事验收 菜鸟Scrum敏捷实践系列(三)用户故事的组织---功能架构的规划 一.用户 ...

  10. html5实例-闪烁的星星

    一.绘制五角星 1.1页面结构 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...