返回介绍

solution / 0900-0999 / 0925.Long Pressed Name / README_EN

发布于 2024-06-17 01:03:33 字数 4050 浏览 0 评论 0 收藏 0

925. Long Pressed Name

中文文档

Description

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 was not in the typed output.

 

Constraints:

  • 1 <= name.length, typed.length <= 1000
  • name and typed consist of only lowercase English letters.

Solutions

Solution 1

class Solution:
  def isLongPressedName(self, name: str, typed: str) -> bool:
    m, n = len(name), len(typed)
    i = j = 0
    while i < m and j < n:
      if name[i] != typed[j]:
        return False
      cnt1 = cnt2 = 0
      c = name[i]
      while i + 1 < m and name[i + 1] == c:
        i += 1
        cnt1 += 1
      while j + 1 < n and typed[j + 1] == c:
        j += 1
        cnt2 += 1
      if cnt1 > cnt2:
        return False
      i, j = i + 1, j + 1
    return i == m and j == n
class Solution {
  public boolean isLongPressedName(String name, String typed) {
    int m = name.length(), n = typed.length();
    int i = 0, j = 0;
    for (; i < m && j < n; ++i, ++j) {
      if (name.charAt(i) != typed.charAt(j)) {
        return false;
      }
      int cnt1 = 0, cnt2 = 0;
      char c = name.charAt(i);
      while (i + 1 < m && name.charAt(i + 1) == c) {
        ++i;
        ++cnt1;
      }
      while (j + 1 < n && typed.charAt(j + 1) == c) {
        ++j;
        ++cnt2;
      }
      if (cnt1 > cnt2) {
        return false;
      }
    }
    return i == m && j == n;
  }
}
class Solution {
public:
  bool isLongPressedName(string name, string typed) {
    int m = name.size(), n = typed.size();
    int i = 0, j = 0;
    for (; i < m && j < n; ++i, ++j) {
      if (name[i] != typed[j]) return false;
      int cnt1 = 0, cnt2 = 0;
      char c = name[i];
      while (i + 1 < m && name[i + 1] == c) {
        ++i;
        ++cnt1;
      }
      while (j + 1 < n && typed[j + 1] == c) {
        ++j;
        ++cnt2;
      }
      if (cnt1 > cnt2) return false;
    }
    return i == m && j == n;
  }
};
func isLongPressedName(name string, typed string) bool {
  m, n := len(name), len(typed)
  i, j := 0, 0
  for ; i < m && j < n; i, j = i+1, j+1 {
    if name[i] != typed[j] {
      return false
    }
    cnt1, cnt2 := 0, 0
    c := name[i]
    for i+1 < m && name[i+1] == c {
      i++
      cnt1++
    }
    for j+1 < n && typed[j+1] == c {
      j++
      cnt2++
    }
    if cnt1 > cnt2 {
      return false
    }
  }
  return i == m && j == n
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文