925. Long Pressed Name

Stella981
• 阅读 661

题目链接: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]的值,该字符变量初始化为空字符

编码如下

 1 class Solution {
 2 public:
 3     bool isLongPressedName(string name, string typed) {
 4         if (name.length() > typed.length()) return false;
 5         
 6         char pre = ' ';
 7         int indexOfName = 0;
 8         
 9         for (int i = 0; i < typed.length(); ++i)
10         {
11             if (name[indexOfName] != typed[i] && pre == ' ')
12                 return false;
13             
14             if (name[indexOfName] == typed[i])
15             {
16                 pre = name[indexOfName];
17                 indexOfName++;
18             }
19             else
20             {
21                 if (pre == typed[i])
22                     continue;
23                 else
24                     return false;
25             }
26         }
27         
28         if (indexOfName != name.length()) return false;
29         
30         return true;  
31     }
32 };
点赞
收藏
评论区
推荐文章
Stella981 Stella981
3年前
LeetCode 0116. 填充每个节点的下一个右侧节点指针【Python】【Go】
ProblemLeetCode(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fleetcode.com%2Fproblems%2Fpopulatingnextrightpointersineachnode%2F)Youaregivenap
Stella981 Stella981
3年前
LeetCode 58. 最后一个单词的长度 (java)
题目:https://leetcodecn.com/problems/lengthoflastword/submissions/(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fleetcodecn.com%2Fproblems%2Flengthoflastw
Stella981 Stella981
3年前
Search Insert Position
\toc\题目链接SearchInsertPositionLeetCode(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fleetcode.com%2Fproblems%2Fsearchinsertposition%2F)注意点
Stella981 Stella981
3年前
LeetCode——295. Find Median from Data Stream
一.题目链接:  https://leetcode.com/problems/findmedianfromdatastream(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fleetcode.com%2Fproblems%2Ffindmedianfromdatast
Stella981 Stella981
3年前
Leetcode 239题 滑动窗口最大值(Sliding Window Maximum) Java语言求解
题目链接https://leetcodecn.com/problems/slidingwindowmaximum/(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fleetcodecn.com%2Fproblems%2Fslidingwindowmaximum%
Stella981 Stella981
3年前
LeetCode 1253. 重构 2 行二进制矩阵
题目链接:https://leetcodecn.com/contest/weeklycontest162/problems/reconstructa2rowbinarymatrix/(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fleetcodecn.com%2Fco
Stella981 Stella981
3年前
LeetCode 1192. Critical Connections in a Network
原题链接在这里:https://leetcode.com/problems/criticalconnectionsinanetwork/(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fleetcode.com%2Fproblems%2Fcriticalconnections
Stella981 Stella981
3年前
LeetCode 39. Combination Sum
问题链接LeetCode39.CombinationSum(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fleetcode.com%2Fproblems%2Fcombinationsum%2Fdescription%2F)题目解析给一组数和一个目
Stella981 Stella981
3年前
851. Loud and Rich —— weekly contest 87
851\.LoudandRich题目链接:https://leetcode.com/problems/loudandrich/description/(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fleetcode.com%2Fpr
Stella981 Stella981
3年前
LeetCode 0090. Subsets II子集II【Python】
ProblemLeetCode(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fleetcode.com%2Fproblems%2Fsubsetsii%2F)Givenacollectionofintegersthatmightcontainduplic