LeetCode 2825. 循环增长使字符串子序列等于另一个字符串
原题链接
中等
作者:
我是java同学
,
2024-04-16 12:49:05
,
所有人可见
,
阅读 8
class Solution {
public:
bool canMakeSubsequence(string str1, string str2) {
int n = str1.size(), m = str2.size();
int i = 0, j = 0;
while (i < n && j < m) {
char c = str1[i];
if (str1[i] == 'z' && str2[j] == 'a') c = 'a';
else if (str1[i] == 'z' && str2[j] == 'z') c = 'z';
if (c == str2[j] || ++ c == str2[j])
j ++ ;
i ++ ;
}
printf("%d %d\n", i, j);
return j == m;
}
};
class Solution:
def canMakeSubsequence(self, str1: str, str2: str) -> bool:
if len(str1) < len(str2):
return False
n, m = len(str1), len(str2)
i = j = 0
while i < n and j < m:
c = chr(ord(str1[i]) + 1) if str1[i] != 'z' else 'a'
if c == str2[j] or str1[i] == str2[j]:
j += 1
i += 1
return j == m