题目链接:https://leetcode.com/problems/long-pressed-name/description/

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:

  1. name.length <= 1000
  2. typed.length <= 1000
  3. The characters of name and typed are lowercase letters.

思路:

  • typed 符合要求,则typed 的 length(长度)满足条件:typed.length() >= name.length();
  • i, j分别是指向nametyped的下标,i, j下标初始值都为0;
  1. name[i] == typed[j] 时,i, j 向后移动一个单位;
  2. name[i] != typed[j] 时,判断 typed[j] 是否等于name[i-1] (name[i-1] == typed[j-1]);
    • typed[j] != name[j-1] 则 typed 不满足,返回false
    • 若 typed[j] == name[i-1], ++j,直至 typed[j] != name[i-1],执行步骤2。

注意:根据上面的分析,需要用一个字符变量来存储name[i]的值,该字符变量初始化为空字符

编码如下

 class Solution {
public:
bool isLongPressedName(string name, string typed) {
if (name.length() > typed.length()) return false; char pre = ' ';
int indexOfName = ; for (int i = ; i < typed.length(); ++i)
{
if (name[indexOfName] != typed[i] && pre == ' ')
return false; if (name[indexOfName] == typed[i])
{
pre = name[indexOfName];
indexOfName++;
}
else
{
if (pre == typed[i])
continue;
else
return false;
}
} if (indexOfName != name.length()) return false; return true;
}
};

925. Long Pressed Name的更多相关文章

  1. 【Leetcode_easy】925. Long Pressed Name

    problem 925. Long Pressed Name solution1: class Solution { public: bool isLongPressedName(string nam ...

  2. [LeetCode] 925. Long Pressed Name 长按键入的名字

    Your friend is typing his name into a keyboard.  Sometimes, when typing a character c, the key might ...

  3. [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 ...

  4. 【leetcode】925.Long Pressed Name

    题目如下: Your friend is typing his name into a keyboard.  Sometimes, when typing a character c, the key ...

  5. 【LeetCode】925. Long Pressed Name 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 参考资料 日期 题目地址:https://leetc ...

  6. leetcode 925. Long Pressed Name

    判定是否长按 var isLongPressedName = function (name, typed) { var i = 1, j = 0, n = name.length, m = typed ...

  7. 算法与数据结构基础 - 双指针(Two Pointers)

    双指针基础 双指针(Two Pointers)是面对数组.链表结构的一种处理技巧.这里“指针”是泛指,不但包括通常意义上的指针,还包括索引.迭代器等可用于遍历的游标. 同方向指针 设定两个指针.从头往 ...

  8. 点击ViewGroup时其子控件也变成pressed状态的原因分析及解决办法

    这个问题,当初在分析touch事件处理的时候按理应该分析到的,可是由于我当时觉得这块代码和touch的主题不是那么紧密, 就这么忽略掉了,直到后来在这上面遇到了问题.其实这个现象做Android开发的 ...

  9. Android根据Button状态(normal,focused,pressed)显示不同背景图片

    Android根据Button状态(normal,focused,pressed)显示不同背景图片 Android中Button 有focused, selected, pressed 等不同状态,通 ...

随机推荐

  1. ***PHP preg_match正则表达式的使用 转载:https://www.cnblogs.com/kenshinobiy/p/4443600.html

    第一,让我们看看两个特别的字符:‘^’和‘$’他们是分别用来匹配字符串的开始和结束,以下分别举例说明 : "^The": 匹配以 "The"开头的字符串; &q ...

  2. vue 2.0 watch 监听对象的变化

  3. 洛谷P1991 无线通讯网【最小生成树】

    题目:https://www.luogu.org/problemnew/show/P1991 题意:有p个点的坐标,可以有s个点使得这s个点之间可以无限制通信. 要使所有点之间两两有通信的路径(可以是 ...

  4. 学到了武沛齐讲的Day14完

    & 交 |   并 ^   并-交 --------------------- 格式化 %s  字符串,数字,一切 %.4s  留前面4位 %d 数字 %f   小数保留6位 四舍五入 %0. ...

  5. PostgreSQL 多版本的实现与Innodb和oracle的差别

    PostgreSQL与oracle或InnoDB的多版本实现最大的区别在于最新版本和历史版本是否分离存储,PostgreSQL不分,而oracle和InnoDB分,而innodb也只是分离了数据,索引 ...

  6. 页面打开excel

    1. File => Stream / MemoryStream FileStream stream = new FileStream(path, FileMode.Open, FileAcce ...

  7. Elasticsearch 索引文档的增删改查

    利用Elasticsearch-head可以在界面上(http://127.0.0.1:9100/)对索引进行增删改查 1.RESTful接口使用方法 为了方便直观我们使用Head插件提供的接口进行演 ...

  8. 「CF375E」Red and Black Tree「树形DP」

    题意 给定一个结点颜色红或黑的树,问最少进行多少次交换黑.红结点使得每个红结点离最近的黑结点距离\(\leq x\). \(1\leq n \leq 500, 1 \leq x \leq 10^9\) ...

  9. 使用scala通过JNI技术调用c++代码

    scala代码编写 Sample1.scala class Sample1 { // --- Native methods @native def intMethod(n: Int): Int def ...

  10. P1021 邮票面值设计——搜索+完全背包

    P1021 邮票面值设计 题目意思是你最多用n张邮票,你可以自己设定k种邮票的面值,每种邮票数量无穷,你最多能用这k种邮票在不超过n张的情况下,组合成的价值要求是从1开始连续的, 求最大能连续到多少: ...