当前位置: 代码迷 >> 综合 >> LeetCode刷题:925. Long Pressed Name
  详细解决方案

LeetCode刷题:925. Long Pressed Name

热度:44   发布时间:2024-01-15 19:32:02.0

LeetCode刷题:925. Long Pressed Name

原题链接:https://leetcode.com/problems/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 and typed are lowercase letters.

你的朋友正在使用键盘输入他的名字 name。偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次。

你将会检查键盘输入的字符 typed。如果它对应的可能是你的朋友的名字(其中一些字符可能被长按),那么就返回 True。


算法设计

public boolean isLongPressedName(String name, String typed) {if(name.length() == 0 && typed.length() == 0){return true;}if(name.length() == 0 || typed.length() == 0){return false;}int n = 0;for(int i = 0; i < typed.length();i++){if(typed.charAt(i) == name.charAt(n)){n++;}if(n == name.length()){return true;}}return false;
}

 

  相关解决方案