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. C#计算表达式(仿计算器功能)

    一.用MSScriptControl在C#中执行JavaScript代码javascript中有个eval方法用过的人都知道他的方便和强大之处.在C#中,我们也可以通过Com组件来执行一段javasc ...

  2. 用C#来学习唐诗三百首

    Begin 最近把项目做完了,闲来无事,就想做点好玩的事情,刚好前几天下载了[唐诗三百首]和[全唐诗]这两个txt文件,正好用C#来整理一下. [唐诗三百首]文件格式 [全唐诗]文件格式 目标 将每一 ...

  3. loadrunner:Auto Correlate自动定位瓶颈

    Auto correlate可以根据数据趋势,自动分析出可能影响目标性能的数据项,通过它可以很方便地找出哪些数据之间有明显的相互依赖性. 下面以分析影响平均响应时间的windows资源为例,讲解Aut ...

  4. 【微信公众平台SDK(链式调用)】经过半个月的迭代,今天抽空写了个Demo

    这个项目是在实际开发中逐渐完善的,开发过程基于ASP.Net Core 1.1,实际生成会兼容Net4.5. 写有完善的代码提示,怎么用就不多做解释了,引用好实例中的命名空间基本上就可以通过智能提示了 ...

  5. android国际化

    语言的国际化 为了提供不同语言的版本,只需要在res中新建几个values文件夹就行 不过文件夹有自己的命名规则 values-语言代码-r国家或者地区的代码 然后我们只需要将不同语言的string. ...

  6. ng-class 用法

    很长时间不写页面很多angualrjs的指令用法忘了,搜搜网上的千篇一律“不全”: 下面我把ngclass的用法列出来!(注:网上只有2种,我补充一种三维运算) 1:   ng-class=" ...

  7. Java中的常量治理

    版权声明:本文为博主原创文章,转载请注明出处,欢迎使劲喷 虽然推崇在java中使用枚举(可查看<Java中的枚举的治理>)来对数据字典及常量进行控制,但是有些时候,我们还是会觉得常量控制更 ...

  8. 多线程爬坑之路-J.U.C.atomic包下的AtomicInteger,AtomicLong等类的源码解析

    Atomic原子类:为基本类型的封装类Boolean,Integer,Long,对象引用等提供原子操作. 一.Atomic包下的所有类如下表: 类摘要 AtomicBoolean 可以用原子方式更新的 ...

  9. xgboost-python参数深入理解

    由于在工作中应用到xgboost做特征训练预测,因此需要深入理解xgboost训练过程中的参数的意思和影响. 通过search,https://www.analyticsvidhya.com/blog ...

  10. SBT使用阿里云Maven仓库,解决SBT下载依赖慢。

    添加:~/.sbt/repositories 文件 文件内容: [repositories] local my-maven-repo: http://maven.aliyun.com/nexus/co ...