Re: [閒聊] 每日leetcode已回收
看板Marginalman作者sustainer123 (caster )時間1年前 (2024/06/03 09:57)推噓4(4推 0噓 2→)留言6則, 5人參與討論串311/1554 (看更多)
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
推
06/03 09:59,
1年前
, 1F
06/03 09:59, 1F
推
06/03 09:59,
1年前
, 2F
06/03 09:59, 2F
推
06/03 10:08,
1年前
, 3F
06/03 10:08, 3F
→
06/03 10:10,
1年前
, 4F
06/03 10:10, 4F
→
06/03 10:19,
1年前
, 5F
06/03 10:19, 5F
推
06/03 14:55,
1年前
, 6F
06/03 14:55, 6F
討論串 (同標題文章)
完整討論串 (本文為第 311 之 1554 篇):