Re: [閒聊] 每日leetcode

看板Marginalman作者 (dont)時間1年前 (2024/12/04 20:38), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1178/1548 (看更多)
2825. Make String a Subsequence Using Cyclic Increments ## 思路 用pointer i 紀錄目前對應的str2字元 掃str1比對對應的str2字元 有match就i+1 如果指標到底就回傳TRUE ## CODE ```python class Solution: def canMakeSubsequence(self, str1: str, str2: str) -> bool: next_char = {} for i in range(25): next_char[chr(ord('a') + i)] = chr(ord('a') + i + 1) next_char['z'] = 'a' i = 0 for ch in str1: if str2[i] == ch or str2[i] == next_char[ch]: i += 1 if i == len(str2): return True return False ``` -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 94.156.205.137 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1733315896.A.F37.html
文章代碼(AID): #1dK4quyt (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1dK4quyt (Marginalman)