作者sustainer123 (caster )
看板Marginalman
標題Re: [閒聊] 每日leetcode
時間Mon Jun 3 09:57:48 2024
https://leetcode.com/problems/append-characters-to-string-to-make-subsequence
2486. Append Characters to String to Make Subsequence
回傳讓t變成s子序列的最小字母數
Example 1:
Input: s = "coaching", t = "coding"
Output: 4
Explanation: Append the characters "ding" to the end of s so that s =
"coachingding".
Now, t is a subsequence of s ("coachingding").
It can be shown that appending any 3 characters to the end of s will never
make t a subsequence.
Example 2:
Input: s = "abcde", t = "a"
Output: 0
Explanation: t is already a subsequence of s ("abcde").
Example 3:
Input: s = "z", t = "abcde"
Output: 5
Explanation: Append the characters "abcde" to the end of s so that s =
"zabcde".
Now, t is a subsequence of s ("zabcde").
It can be shown that appending any 4 characters to the end of s will never
make t a subsequence.
Constraints:
1 <= s.length, t.length <= 105
s and t consist only of lowercase English letters.
思路:
two pointer
Python Code:
class Solution:
def appendCharacters(self, s: str, t: str) -> int:
left = 0
right = 0
while left < len(s) and right < len(t):
if s[left] == t[right]:
right += 1
left += 1
return len(t[right:])
本來想說還可以用字典樹
搓完才發現有點問題
姆咪
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.137.48.144 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1717379871.A.6C8.html
推 DJYOSHITAKA: 別捲了 06/03 09:59
推 JIWP: 別卷了 06/03 09:59
推 JIWP: 這題應該放easy 06/03 10:08
→ sustainer123: 確實 我原本以為有啥陷阱 結果就這樣而已 06/03 10:10
→ SecondRun: 大師 06/03 10:19
推 orangeNoob: 別捲了 06/03 14:55