返回介绍

solution / 2800-2899 / 2825.Make String a Subsequence Using Cyclic Increments / README_EN

发布于 2024-06-17 01:02:59 字数 5078 浏览 0 评论 0 收藏 0

2825. Make String a Subsequence Using Cyclic Increments

中文文档

Description

You are given two 0-indexed strings str1 and str2.

In an operation, you select a set of indices in str1, and for each index i in the set, increment str1[i] to the next character cyclically. That is 'a' becomes 'b', 'b' becomes 'c', and so on, and 'z' becomes 'a'.

Return true _if it is possible to make _str2 _a subsequence of _str1 _by performing the operation at most once_, _and_ false _otherwise_.

Note: A subsequence of a string is a new string that is formed from the original string by deleting some (possibly none) of the characters without disturbing the relative positions of the remaining characters.

 

Example 1:

Input: str1 = "abc", str2 = "ad"
Output: true
Explanation: Select index 2 in str1.
Increment str1[2] to become 'd'. 
Hence, str1 becomes "abd" and str2 is now a subsequence. Therefore, true is returned.

Example 2:

Input: str1 = "zc", str2 = "ad"
Output: true
Explanation: Select indices 0 and 1 in str1. 
Increment str1[0] to become 'a'. 
Increment str1[1] to become 'd'. 
Hence, str1 becomes "ad" and str2 is now a subsequence. Therefore, true is returned.

Example 3:

Input: str1 = "ab", str2 = "d"
Output: false
Explanation: In this example, it can be shown that it is impossible to make str2 a subsequence of str1 using the operation at most once. 
Therefore, false is returned.

 

Constraints:

  • 1 <= str1.length <= 105
  • 1 <= str2.length <= 105
  • str1 and str2 consist of only lowercase English letters.

Solutions

Solution 1: Two Pointers

This problem actually requires us to determine whether a string $s$ is a subsequence of another string $t$. However, the characters do not have to match exactly. If two characters are the same, or one character is the next character of the other, they can match.

The time complexity is $O(m + n)$, where $m$ and $n$ are the lengths of the strings $str1$ and $str2$ respectively. The space complexity is $O(1)$.

class Solution:
  def canMakeSubsequence(self, str1: str, str2: str) -> bool:
    i = 0
    for c in str1:
      d = "a" if c == "z" else chr(ord(c) + 1)
      if i < len(str2) and str2[i] in (c, d):
        i += 1
    return i == len(str2)
class Solution {
  public boolean canMakeSubsequence(String str1, String str2) {
    int i = 0, n = str2.length();
    for (char c : str1.toCharArray()) {
      char d = c == 'z' ? 'a' : (char) (c + 1);
      if (i < n && (str2.charAt(i) == c || str2.charAt(i) == d)) {
        ++i;
      }
    }
    return i == n;
  }
}
class Solution {
public:
  bool canMakeSubsequence(string str1, string str2) {
    int i = 0, n = str2.size();
    for (char c : str1) {
      char d = c == 'z' ? 'a' : c + 1;
      if (i < n && (str2[i] == c || str2[i] == d)) {
        ++i;
      }
    }
    return i == n;
  }
};
func canMakeSubsequence(str1 string, str2 string) bool {
  i, n := 0, len(str2)
  for _, c := range str1 {
    d := byte('a')
    if c != 'z' {
      d = byte(c + 1)
    }
    if i < n && (str2[i] == byte(c) || str2[i] == d) {
      i++
    }
  }
  return i == n
}
function canMakeSubsequence(str1: string, str2: string): boolean {
  let i = 0;
  const n = str2.length;
  for (const c of str1) {
    const d = c === 'z' ? 'a' : String.fromCharCode(c.charCodeAt(0) + 1);
    if (i < n && (str2[i] === c || str2[i] === d)) {
      ++i;
    }
  }
  return i === n;
}

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

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

发布评论

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