[LeetCode] 925. Long Pressed Name 长按键入的名字
Your friend is typing his `name` into a keyboard. Sometimes, when typing a character `c`, the key might get *long pressed*, and the character will be typed 1 or more times.
You examine the typed
characters of the keyboard. Return True
if it is possible that it was your friends name, with some characters (possibly none) being long pressed.
Example 1:
Input: name = "alex", typed = "aaleex"
Output: true
Explanation: 'a' and 'e' in 'alex' were long pressed.
Example 2:
Input: name = "saeed", typed = "ssaaedd"
Output: false
Explanation: 'e' must have been pressed twice, but it wasn't in the typed output.
Example 3:
Input: name = "leelee", typed = "lleeelee"
Output: true
Example 4:
Input: name = "laiden", typed = "laiden"
Output: true
Explanation: It's not necessary to long press any character.
Note:
name.length <= 1000
typed.length <= 1000
- The characters of
name
andtyped
are lowercase letters.
这道题说是你朋友在用键盘敲入名字的时候,对于某个字母可能会长时间按键,这导可能会有多个相同字母输入,这种情况是允许的,现在给了两个字符串,name 是朋友的名字,typed 是朋友敲入的字符串,问 typed 是否是朋友敲入的名字。其实这道题的本质是,对于 name 中每个位置的字母,对应在 typed 中的出现次数一定要相等或者更多,但是直接统计每个字符出现的次数是不对的,因为位置关系很重要,比如 abb 和 abab,虽然后者中a和b的出现次数都大于等于前者,但还是要返回 false。博主最先想的方法是用两个指针i和j分别提取 name 和 typed 字符串中每个字母出现的次数,如果 typed 中的次数小于 name 中的次数,则直接返回 false 即可,最终循环结束后,i和j应该分别为 name 和 typed 的长度,此时返回 true,否则返回 false,参见代码如下:
解法一:
class Solution {
public:
bool isLongPressedName(string name, string typed) {
int i = 0, j = 0, m = name.size(), n = typed.size();
while (i < m || j < n) {
int start1 = i, start2 = j;
while (i < m - 1 && name[i] == name[i + 1]) ++i;
while (j < n - 1 && typed[j] == typed[j + 1]) ++j;
if (j - start2 + 1 < i - start1 + 1) return false;
++i; ++j;
}
return i == m && j == n;
}
};
可以写的简洁一些,不需要使用那么多的 while 循环,而是直接用j遍历 typed 中每个字母,i初识时指向 name 的第一个字母,假如i小于m,且 name[i] 等于 typed[j-1] 时,则i自增1,否则的话,若此时j为0(说明第一个字母就不匹配),或者 typed[j] 不等于 typed[j - 1](说明出现了无法匹配的新字母),则直接返回 false。循环退出后若i等于m则返回 true,否则返回 false,参见代码如下:
解法二:
class Solution {
public:
bool isLongPressedName(string name, string typed) {
int i = 0, m = name.size(), n = typed.size();
for (int j = 0; j < n; ++j) {
if (i < m && name[i] == typed[j]) ++i;
else if (!j || typed[j] != typed[j - 1]) return false;
}
return i == m;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/925
参考资料:
https://leetcode.com/problems/long-pressed-name/
https://leetcode.com/problems/long-pressed-name/discuss/183994/C%2B%2BJavaPython-Two-Pointers
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[LeetCode] 925. Long Pressed Name 长按键入的名字的更多相关文章
- Leetcode925.Long Pressed Name长按键入
你的朋友正在使用键盘输入他的名字 name.偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次. 你将会检查键盘输入的字符 typed.如果它对应的可能是你的朋友的名字(其中一 ...
- leetcode 925. Long Pressed Name
判定是否长按 var isLongPressedName = function (name, typed) { var i = 1, j = 0, n = name.length, m = typed ...
- 【Leetcode_easy】925. Long Pressed Name
problem 925. Long Pressed Name solution1: class Solution { public: bool isLongPressedName(string nam ...
- 【python】Leetcode每日一题-最长公共子序列
[python]Leetcode每日一题-最长公共子序列 [题目描述] 给定两个字符串 text1 和 text2,返回这两个字符串的最长 公共子序列 的长度.如果不存在 公共子序列 ,返回 0 . ...
- leetcode 925. 长按键入
题目描述: 你的朋友正在使用键盘输入他的名字 name.偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次. 你将会检查键盘输入的字符 typed.如果它对应的可能是你的朋友的 ...
- [Swift]LeetCode925. 长按键入 | Long Pressed Name
Your friend is typing his name into a keyboard. Sometimes, when typing a character c, the key might ...
- 力扣(LeetCode)长按键入 个人题解
你的朋友正在使用键盘输入他的名字 name.偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次. 你将会检查键盘输入的字符 typed.如果它对应的可能是你的朋友的名字(其中一 ...
- 【LeetCode】925. Long Pressed Name 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 参考资料 日期 题目地址:https://leetc ...
- [LeetCode&Python] Problem 925. Long Pressed Name
Your friend is typing his name into a keyboard. Sometimes, when typing a character c, the key might ...
随机推荐
- Mac终端常用快捷键
Ctrl + a 跳到行首Ctrl + e 跳到行尾Ctrl + d 删除一个字符,相当于通常的Delete键(命令行若无所有字符,则相当于exit:处理多行标准输入时也表示eof)Ctrl + h ...
- 如何给gridControl动态的添加合计
for (int i = 0; i < this.dsHz.Tables[0].Columns.Count; i++) { if (dsHz.Tables[0].Columns[i].DataT ...
- centos6利用cgroup冻结一个程序运行
操作步骤: 安装cgroup服务 yum install libcgroup 配置cgroup vim /etc/cgconfig.conf group stopit{ #添加一个cgroup组 fr ...
- C# 方法的out、ref、params参数
一.out参数实例 [实例]求一个数组中的最大值.最小值.总和.平均值 class Program { static void Main(string[] args) { //写一个方法 求一个数组中 ...
- 反射与类对象获取-Java学习
类对象 类对象指的是一个类在jvm中加载后所形成的对象,每一个类都只有一个类对象,该类对象被所有的实例对象所共享. 类之间有不同的方法,不同的属性.类对象,就是用于描述这种类,都有什么属性,什么方法的 ...
- 基础系列(2)--- css1
css组成 css语法组成 选择器 和 声明 (多个声明用分号隔开) 声明包括 属性和属性值(多个属性值用空格隔开) 语法: 选择器{ 属性: 属性值; 属性: 属性值1 属性值2; } css样式表 ...
- 英语rhodita铑金RHODITA单词
铑金RHODITA,铑属铂系元素.铂系元素几乎完全成单质状态存在,高度分散在各种矿石中,例如原铂矿.硫化镍铜矿.磁铁矿等.铂系元素几乎无例外地共同存在,形成天然合金.在含铂系元素矿石中,通常以铂为主要 ...
- pid相关命令
pidof 查找正在运行进程的进程号(pid)的工具 pidof - find the process ID of a running program 参数: -s 表示只返回1个 pid -x 表示 ...
- 深入理解JVM虚拟机(一):JVM运行时数据区
概述: JVM将内存的管理进行封装,使得开发人员不必关心内存申请.释放操作.但是在高级程序开发.复杂业务场景开发的时候,如果出现内存溢出的情况,对于开发人员而言就很难去分析出原因.所以还是很有必要去了 ...
- 【PHP】关于系统性能追踪工具molten
一.简介 关于molten的介绍网上有很多,是一个全链路追踪的工具,Molten可以看做是phptrace的的升级版(流行的php问题定位工具譬如phptrace,xhprof,这些工具可以自行Goo ...