Leetcode # 2825. Make String a Subsequence Using Cyclic Increments
- 2024.12.05
- ★★ Medium LeetCode Two Pointers
Problem
https://leetcode.com/problems/make-string-a-subsequence-using-cyclic-increments
Solution
使用try-except
避免多次的p1
的out of index
檢查
Time Complexity: O()
Space Complexity: O()
(The input and output generally do not count towards the space complexity.)
class Solution: def canMakeSubsequence(self, str1: str, str2: str) -> bool: p1 = 0 try: for p2, c2 in enumerate(str2): pre_c2 = chr((ord(c2) - 97 - 1) % 26 + 97) while (c1 := str1[p1]) != c2 and c1 != pre_c2: p1 += 1 p1 += 1 except IndexError as e: return False return True
Last Updated on 2024/12/05 by A1go