原题链接在这里:https://leetcode.com/problems/keyboard-row/#/description

题目:

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.

题解:

把在同row的char标记成同index存在HashMap<Character, Integer> hm中.

然后判定word中的每一个char是否有相同的index.

Time Complexity: O(n), n是words中每个word的char总和.

Space: O(1). hm size, regardless res size.

AC Java:

 1 public class Solution {
 2     public String[] findWords(String[] words) {
 3         if(words == null || words.length == 0){
 4             return words;
 5         }
 6
 7         String [] keyboard = {"qwertyuiop", "asdfghjkl", "zxcvbnm"};
 8         HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
 9         for(int i = 0; i<keyboard.length; i++){
10             for(char c : keyboard[i].toCharArray()){
11                 hm.put(c, i);
12             }
13         }
14
15         List<String> res = new ArrayList<String>();
16         for(String word : words){
17             if(word == null || word.length() == 0){
18                 continue;
19             }
20             int ind = hm.get(word.toLowerCase().charAt(0));
21             for(char c : word.toLowerCase().toCharArray()){
22                 if(ind != hm.get(c)){
23                     ind = -1;
24                 }
25             }
26             if(ind != -1){
27                 res.add(word);
28             }
29         }
30
31         return res.toArray(new String[0]);
32     }
33 }

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/6637828.html

LeetCode Keyboard Row相关推荐

  1. Leetcode PHP题解--D25 500. Keyboard Row

    500. Keyboard Row 题目链接 500. Keyboard Row 题目分析 给定一个字符串数组,返回那些所出现的字母在QWERTY键盘中同一行的字符串. 例如,单词hello中,字母h ...

  2. 【leetcode】500. Keyboard Row

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

  3. LeetCode之Keyboard Row

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

  4. LeetCode 500. Keyboard Row

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

  5. C#LeetCode刷题之#500-键盘行(Keyboard Row)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3796 访问. 给定一个单词列表,只返回可以使用在键盘同一行的字母 ...

  6. 500. Keyboard Row

    此题要求在input string array中找出能被一行键盘打出来的string,很有意思的一题,有很多amazing的解法: Solution 1:一行函数解爆它 public String[] ...

  7. C#LeetCode刷题-哈希表

    哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串   24.2% 中等 18 四数之和   ...

  8. LeetCode 简单算法题

    使用Nodejs 抓取的LeetCode 简单算法题  一步一步来,先攻破所有简单的题目,有些题目不适合使用JS解决,请自行斟酌 Letcode 简单题汇总 104. Maximum Depth of ...

  9. 【Leetcode】精选算法top200道(二)

    二.中等 339.嵌套列表权重和 给定一个嵌套的整数列表 nestedList ,每个元素要么是整数,要么是列表.同时,列表中元素同样也可以是整数或者是另一个列表. 整数的 深度 是其在列表内部的嵌套 ...

最新文章

  1. Linux运维工程师可是很吃香的
  2. Spring Boot 2.x基础教程:JSR-303实现请求参数校验
  3. kafka安装_安装Kafka
  4. Parse a document from a String
  5. 关于bochs用X11启动的说明
  6. Visual Studio 2010 中的 SharePoint 开发
  7. TCP为何采用三次握手来建立连接,若采用二次握手可以吗
  8. Linux 的 chmod 命令,对一个目录及其子目录所有文件添加权限
  9. 2021知识类内容研究报告
  10. 10个必会的 PyCharm 技巧,附高清大图
  11. linux不停机释放日志,Kafka在线修改topic日志保存时长(不停机,不重启)
  12. 清华大学计算机夏文韬,太猛了--南京外国语学校2007届高三毕业生去向
  13. Windows 7 频繁提示:计算机的内存不足
  14. UNITY_iOS_接入微信登录及分享微信
  15. Java、JSP 基于微信小程序 宠物在线医疗管理系统的设计与实现
  16. 数据库设计-多级栏目(标题)分类设计
  17. 平面设计中的插画设计技巧
  18. [fyne] build constraints exclude all Go files in
  19. java 方法规范_Java规范推荐
  20. Android IO流程你真的清楚了吗|硬核科普

热门文章

  1. xml批量修改php,通过php修改xml文档内容的方法
  2. [Gamma]Scrum Meeting#5
  3. xcode 中 的工程模板
  4. Quartz.NET和Log4Net三种输出[转]
  5. 错误笔记(1)——关于克隆虚拟机引发的后续问题
  6. Apache经常使用配置
  7. 一个功能函数所具备的要素
  8. MVC与Validate验证提示的样式修改
  9. ASP.NET设置ie打印两法 (精)(转)
  10. Web 服务器 之 安装 Apache 2 和 Tomcat 5.5