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. 如何垂直居中<img>?

    方法1: 父元素设置height=line-height,子元素设置vertical-align:middle; 方法2: 父元素display:table-cell;vertical-align:m ...

  2. win7下使用git

    1 安装git for window 2 安装tortoiseGit 3 生成public key 3.1 打开git bash 3.2 创建~/.ssh文件夹 mkdir ~/.ssh 3.3 配置 ...

  3. JavaScript学习12 JS中定义对象的几种方式【转】

    avaScript学习12 JS中定义对象的几种方式 转自:  http://www.cnblogs.com/mengdd/p/3697255.html JavaScript中没有类的概念,只有对象. ...

  4. 基于requirejs+bluebird,50行代码实现轻巧实用的前端CMD加载器

    首先是github地址,可以用git克隆命令也可以直接在git页面下载 https://github.com/kazetotori/js-requireAsync 下载下来后目录结构是这样的 -pac ...

  5. 3893: [Usaco2014 Dec]Cow Jog

    3893: [Usaco2014 Dec]Cow Jog Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 174  Solved: 87[Submit] ...

  6. 算法模板——线段树6(二维线段树:区域加法+区域求和)(求助phile)

    实现功能——对于一个N×M的方格,1:输入一个区域,将此区域全部值作加法:2:输入一个区域,求此区域全部值的和 其实和一维线段树同理,只是不知道为什么速度比想象的慢那么多,求解释...@acphile ...

  7. python 接口自动化测试(一)

    一.测试需求描述 对服务后台一系列SOAP接口功能测试 参数传入:根据接口描述构造不同的参数输入值(Json格式) 二.程序设计 通过Excel配置具体的测试用例数据 保存参数为Json格式,预写入预 ...

  8. Ubuntu 16.04安装DB2 Express C v11.1

    欢迎和大家交流技术相关问题:邮箱: jiangxinnju@163.com博客园地址: http://www.cnblogs.com/jiangxinnjuGitHub地址: https://gith ...

  9. Omi框架Store体系的前世今生

    原文链接-https://github.com/AlloyTeam/omi Store 体系 先说说Store系统是干什么的!为什么要造这样一个东西?能够系统架构带来什么? 当我们组件之间,拥有共享的 ...

  10. CSS.04 -- 浮动float、overflow、定位position、CSS初始化

    标准流:行内/行内块元素横向有序排列 : 块元素纵向有序排列. 浮动:Float 语法:float:left/right :  设置浮动的元素,脱离标准流 浮动的框可以向左或向右移动,直到它的外边缘碰 ...