题目链接: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. LightOJ - 1214-Large Division(数学,同余)

    链接: https://vjudge.net/problem/LightOJ-1214 题意: Given two integers, a and b, you should check whethe ...

  2. navigator对象及属性(userAgent)(扩展)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. vue子父组件传值

    https://blog.csdn.net/weixin_38888773/article/details/81902789 https://blog.csdn.net/jsxiaoshu/artic ...

  4. 使用这些 CSS 属性选择器来提高前端开发效率

    属性选择器非常神奇.它们可以使你摆脱棘手的问题,帮助你避免添加类,并指出代码中的一些问题.但是不要担心,虽然属性选择器非常复杂和强大,但是它们很容易学习和使用.在本文中,我们将讨论它们是如何运行的,并 ...

  5. ES WIndows 安装 ES与ES-head

    一.ES的安装 1.到ES官网下载ES 安装ES前,需要安装JDK1.8以上版本 https://www.elastic.co/downloads/elasticsearch 2.解压ES 3.安装E ...

  6. 066_调整虚拟机内存参数的 shell 脚本

    #!/bin/bash#脚本通过调用 virsh 命令实现对虚拟机的管理,如果没有该命令,需要安装 libvirt-client 软件包 cat << EOF1.调整虚拟机最大内存数值2. ...

  7. leetcode解题报告(23):Pascal's Triangle

    描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, R ...

  8. 洛谷P1016 旅行家的预算 题解

    主要就是注意一下各个变量的类型别弄混了 https://www.luogu.org/problem/P1016 #include<cstdio> using namespace std; ...

  9. P1613 跑路——倍增思想,floyd

    https://www.luogu.org/problemnew/show/P1613 他有一个跑路机器,每次只能跑2k   (单位)路程,每相邻两个点的路程为1,也就是说如果连边1——>2—— ...

  10. 【00NOIP提高组】单词接龙

    #include<bits/stdc++.h> using namespace std; ; int n,length; int vis[N]; string str[N]; inline ...