欢迎光临散文网 会员登陆 & 注册

LeetCode 2825. Make String a Subsequence Using Cyclic Increments

2023-08-20 08:58 作者:您是打尖儿还是住店呢  | 我要投稿

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.

-----------------------------------------


给定两个 0 索引的字符串 str1 和 str2。


在操作中,您在 str1 中选择一组索引,并且对于该组中的每个索引 i,将 str1[i] 循环递增到下一个字符。 即“a”变为“b”,“b”变为“c”,依此类推,“z”变为“a”。


如果最多执行一次操作可以使 str2 成为 str1 的子序列,则返回 true,否则返回 false。


注意:字符串的子序列是通过删除一些(可能没有)字符而不影响剩余字符的相对位置而从原始字符串形成的新字符串。

利用双指针做法即可;最后判断str2的指针是否等于字符串的长度;


Runtime: 7 ms, faster than 100.00% of Java online submissions for Make String a Subsequence Using Cyclic Increments.

Memory Usage: 44.5 MB, less than 100.00% of Java online submissions for Make String a Subsequence Using Cyclic Increments.


LeetCode 2825. Make String a Subsequence Using Cyclic Increments的评论 (共 条)

分享到微博请遵守国家法律