返回介绍

solution / 2400-2499 / 2486.Append Characters to String to Make Subsequence / README_EN

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

2486. Append Characters to String to Make Subsequence

中文文档

Description

You are given two strings s and t consisting of only lowercase English letters.

Return _the minimum number of characters that need to be appended to the end of _s_ so that _t_ becomes a subsequence of _s.

A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.

 

Example 1:

Input: s = "coaching", t = "coding"
Output: 4
Explanation: Append the characters "ding" to the end of s so that s = "coachingding".
Now, t is a subsequence of s ("coachingding").
It can be shown that appending any 3 characters to the end of s will never make t a subsequence.

Example 2:

Input: s = "abcde", t = "a"
Output: 0
Explanation: t is already a subsequence of s ("abcde").

Example 3:

Input: s = "z", t = "abcde"
Output: 5
Explanation: Append the characters "abcde" to the end of s so that s = "zabcde".
Now, t is a subsequence of s ("zabcde").
It can be shown that appending any 4 characters to the end of s will never make t a subsequence.

 

Constraints:

  • 1 <= s.length, t.length <= 105
  • s and t consist only of lowercase English letters.

Solutions

Solution 1: Two Pointers

We define two pointers $i$ and $j$, pointing to the first characters of strings $s$ and $t$ respectively. We traverse string $t$, when $s[i] \neq t[j]$, we move pointer $i$ forward until $s[i] = t[j]$ or $i$ reaches the end of string $s$. If $i$ reaches the end of string $s$, it means that the character $t[j]$ in $t$ cannot find the corresponding character in $s$, so we return the remaining number of characters in $t$. Otherwise, we move both pointers $i$ and $j$ forward and continue to traverse string $t$.

The time complexity is $O(m + n)$, and the space complexity is $O(1)$. Where $m$ and $n$ are the lengths of strings $s$ and $t$ respectively.

class Solution:
  def appendCharacters(self, s: str, t: str) -> int:
    i, m = 0, len(s)
    for j, c in enumerate(t):
      while i < m and s[i] != c:
        i += 1
      if i == m:
        return len(t) - j
      i += 1
    return 0
class Solution {
  public int appendCharacters(String s, String t) {
    int m = s.length(), n = t.length();
    for (int i = 0, j = 0; j < n; ++j) {
      while (i < m && s.charAt(i) != t.charAt(j)) {
        ++i;
      }
      if (i++ == m) {
        return n - j;
      }
    }
    return 0;
  }
}
class Solution {
public:
  int appendCharacters(string s, string t) {
    int m = s.size(), n = t.size();
    for (int i = 0, j = 0; j < n; ++j) {
      while (i < m && s[i] != t[j]) {
        ++i;
      }
      if (i++ == m) {
        return n - j;
      }
    }
    return 0;
  }
};
func appendCharacters(s string, t string) int {
  m, n := len(s), len(t)
  for i, j := 0, 0; j < n; i, j = i+1, j+1 {
    for i < m && s[i] != t[j] {
      i++
    }
    if i == m {
      return n - j
    }
  }
  return 0
}
function appendCharacters(s: string, t: string): number {
  const [m, n] = [s.length, t.length];
  for (let i = 0, j = 0; j < n; ++j) {
    while (i < m && s[i] !== t[j]) {
      ++i;
    }
    if (i === m) {
      return n - j;
    }
    ++i;
  }
  return 0;
}

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

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

发布评论

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