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. DES加密解密与AES加密解密

    随着开发时间的变长,当初认为比较难的东西,现在渐渐也就变的不那么难了!特别对于一些经常很少使用的类,时间长了之后渐渐就陌生了.所以在这里写一些日后可能会用到的加密与解密. 一.AES加密算法和DES加 ...

  2. DTD验证XML文档

    DTD验证XML文档        1.DTD简介:DTD是Document Type Definition的缩写,即文档定义            1.1:DTD的内容包含:             ...

  3. 手把手教你webpack、react和node.js环境配置(上篇)

    很多人刚学习react的时候,往往因为繁琐的配置而头疼,这里我将手把手教大家怎么用webpack配置react和redux的环境,这篇教程包括前端react和后台node整个网站的环境配置,对node ...

  4. 去掉Dedecms幻灯片的标题文字和绿色背景

    在操作dedecms站群中的站,因为看到首页的幻灯片Flash显示标题和文字,显得有点突兀,觉得应该去除dedecms 幻灯片默认的文字标题和背景,这样会感觉舒服些,下面教大家如何在模板当中去除ded ...

  5. jquery考试纠错笔记.

    1. 获取元素范围大小顺序依次为: $(#one).siblings("div")>$("#one~div")>$("#one +div& ...

  6. Cocos2d-x中的CC_CALLBACK_X详解

    Cocos2d-x 3.x版本用CC_CALLBACK_0,CC_CALLBACK_1,CC_CALLBACK_2和CC_CALLBACK_3的宏来定义回调方法类的. 3.x版本的例子: child- ...

  7. Selenium 显示等待和隐式等待

    1. 设置显示等待 Java代码: 1 2 3 4 5 public static WebElement WaitForElement(WebDriver driver, String locator ...

  8. ASP从HTML标签中提取中文

    Function delHtml(strHtml) '做了一个函数名叫delhtml Dim objRegExp, strOutput Set objRegExp = New Regexp ' 建立正 ...

  9. OpenFlow硬件交换机制作及刷机教程

    1.目的 将普通路由器升级成为一台支持OpenFlow的交换机. 具体哪些路由器可以刷OpenFlow可以参考:OpenWRT:http://wiki.openwrt.org/toh/start#su ...

  10. 关于在链路聚合下做smart,monitor link的转发测试

    关于在链路聚合下做smart,monitor link的转发测试 这次又要开始瞎折腾了.没事找事了.毕竟我还是很无聊的: 这个实验主要要涉及到以下几个方面: 1.gvrp的应用: 2.Smart Li ...