Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【每日一题】- 2020-01-15 - 925. 长按键入 #276

Open
azl397985856 opened this issue Jan 15, 2020 · 4 comments
Open

【每日一题】- 2020-01-15 - 925. 长按键入 #276

azl397985856 opened this issue Jan 15, 2020 · 4 comments

Comments

@azl397985856
Copy link
Owner

@azl397985856 azl397985856 commented Jan 15, 2020

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

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

 

示例 1:

输入:name = "alex", typed = "aaleex"
输出:true
解释:'alex' 中的 'a' 和 'e' 被长按。
示例 2:

输入:name = "saeed", typed = "ssaaedd"
输出:false
解释:'e' 一定需要被键入两次,但在 typed 的输出中不是这样。
示例 3:

输入:name = "leelee", typed = "lleeelee"
输出:true
示例 4:

输入:name = "laiden", typed = "laiden"
输出:true
解释:长按名字中的字符并不是必要的。
 

提示:

name.length <= 1000
typed.length <= 1000
name 和 typed 的字符都是小写字母。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/long-pressed-name
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

@unclegem

This comment has been minimized.

Copy link

@unclegem unclegem commented Jan 15, 2020

控制双指针对两个字符串进行字符比对即可。

    public boolean isLongPressedName(String name, String typed) {

        if (typed.length() < name.length())
            return false;
        int left = 0, right = 0;

        while (left < name.length() && right < typed.length()) {

            if (name.charAt(left) == typed.charAt(right)) {

                left++;
                right++;
            } else if (right > 0 && typed.charAt(right) == typed.charAt(right - 1))
                right++;
            else
                return false;
        }

        return left == name.length();
    }
@azl397985856

This comment has been minimized.

Copy link
Owner Author

@azl397985856 azl397985856 commented Jan 15, 2020

@unclegem same idea Python Code

class Solution:
    def isLongPressedName(self, name: str, typed: str) -> bool:
        i = j = 0
        m = len(name)
        n = len(typed)
        if m > n:
            return False
        while i < m and j < n:
            if name[i] == typed[j]:
                i += 1
                j += 1
            elif typed[j] == typed[j - 1]:
                j += 1
            else:
                return False

        return i == m
@hhbszyc001

This comment has been minimized.

Copy link

@hhbszyc001 hhbszyc001 commented Jan 26, 2020

public boolean isLongPressedName(String name, String typed) {

    if (typed.length() < name.length())
        return false;
    int left = 0, right = 0;

    while (left < name.length() && right < typed.length()) {

        if (name.charAt(left) == typed.charAt(right)) {

            left++;
            right++;
        } else if (right > 0 && typed.charAt(right) == typed.charAt(right - 1))
            right++;
        else
            return false;
    }

    return left == name.length();
}
@azl397985856

This comment has been minimized.

Copy link
Owner Author

@azl397985856 azl397985856 commented Jan 27, 2020

@hhbszyc001 注意下格式

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.