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