Re: [閒聊] 每日leetcode

作者: sustainer123 (caster)   2024-06-03 09:57:48
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:])
本來想說還可以用字典樹
搓完才發現有點問題
姆咪
作者: DJYOSHITAKA (Evans)   2024-06-03 09:59:00
別捲了
作者: JIWP (JIWP)   2024-06-03 09:59:00
別卷了這題應該放easy
作者: sustainer123 (caster)   2024-06-03 10:10:00
確實 我原本以為有啥陷阱 結果就這樣而已
作者: SecondRun (雨夜琴聲)   2024-06-03 10:19:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com