理論部分
- 用數組實現一個順序的串結構。
- 爲該串結構提供豐富的操作,比如插入子串、在指定位置移除給定長度的子串、在指定位置取子串、連接串、串匹配等。
練習部分
- 無重複字符的最長子串
https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
給定一個字符串,請你找出其中不含有重複字符的最長子串的長度。
示例1
輸入: "abcabcbb"
輸出: 3
解釋: 因爲無重複字符的最長子串是 "abc",所以其長度爲 3。
示例2
輸入: "bbbbb"
輸出: 1
解釋: 因爲無重複字符的最長子串是 "b",所以其長度爲 1。
示例3
輸入: "pwwkew"
輸出: 3
解釋: 因爲無重複字符的最長子串是 "wke",所以其長度爲 3。
請注意,你的答案必須是 子串 的長度,"pwke" 是一個子序列,不是子串。
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
longest_str = []
max_size = [0]
for i in s:
if i not in longest_str:
longest_str.append(i)
max_size.append(len(longest_str))
else:
max_size.append(len(longest_str))
index = longest_str.index(i)
del longest_str[0: index+1]
longest_str.append(i)
return max(max_size)
- 串聯所有單詞的子串
https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/
給定一個字符串s和一些長度相同的單詞 words。找出 s 中恰好可以由 words 中所有單詞串聯形成的子串的起始位置。
注意子串要與 words 中的單詞完全匹配,中間不能有其他字符,但不需要考慮 words 中單詞串聯的順序。
示例1
輸入:
s = "barfoothefoobarman",
words = ["foo","bar"]
輸出:[0,9]
解釋:
從索引 0 和 9 開始的子串分別是 "barfoo" 和 "foobar" 。輸出的順序不重要, [9,0] 也是有效答案。
示例2
輸入:
s = "wordgoodgoodgoodbestword",
words = ["word","good","best","word"]
輸出:[]
from collections import Counter
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
if not s or not words:
return []
one_word = len(words[0])
word_num = len(words)
n = len(s)
if n < one_word:
return []
words = Counter(words)
res = []
for i in range(0, one_word):
cur_cnt = 0
left = i
right = i
cur_Counter = Counter()
while right + one_word <= n:
w = s[right:right + one_word]
right += one_word
if w not in words:
left = right
cur_Counter.clear()
cur_cnt = 0
else:
cur_Counter[w] += 1
cur_cnt += 1
while cur_Counter[w] > words[w]:
left_w = s[left:left+one_word]
left += one_word
cur_Counter[left_w] -= 1
cur_cnt -= 1
if cur_cnt == word_num :
res.append(left)
return res
- 替換子串得到平衡字符串
https://leetcode-cn.com/problems/replace-the-substring-for-balanced-string/
有一個只含有’Q’, ‘W’, ‘E’,'R’四種字符,且長度爲 n的字符串。假如在該字符串中,這四個字符都恰好出現n/4次,那麼它就是一個「平衡字符串」。
給你一個這樣的字符串 s,請通過「替換一個子串」的方式,使原字符串 s變成一個「平衡字符串」。你可以用和「待替換子串」長度相同的任何其他字符串來完成替換。
請返回待替換子串的最小可能長度。
如果原字符串自身就是一個平衡字符串,則返回 0。
示例1:
輸入:s = "QWER"
輸出:0
解釋:s 已經是平衡的了。
示例2:
輸入:s = "QQWE"
輸出:1
解釋:我們需要把一個 'Q' 替換成 'R',這樣得到的 "RQWE" (或 "QRWE") 是平衡的。
示例3:
輸入:s = "QQQW"
輸出:2
解釋:我們可以把前面的 "QQ" 替換成 "ER"。
示例4:
輸入:s = "QQQQ"
輸出:3
解釋:我們可以替換後 3 個 'Q',使 s = "QWER"。
有點問題
class Solution:
def balancedString(self, s: str) -> int:
q_count = s.count('Q')
w_count = s.count('W')
e_count = s.count('E')
r_count = s.count('R')
all_count = []
all_count.append(q_count)
all_count.append(w_count)
all_count.append(e_count)
all_count.append(r_count)
counts = 0
if q_count != len(s) / 4:
counts += 1
if w_count != len(s) / 4:
counts += 1
if e_count != len(s) / 4:
counts += 1
if r_count != len(s) / 4:
counts += 1
min_count = min(all_count)
if min_count:
if counts - 1 >= 0:
return counts - 1
else:
return 0
else:
all_count.pop(min_count)
min_count = min(all_count)
all_count = all_count.map(lambda x: x-min_count, all_count)
if min_count:
if counts - 1 >= 0:
return counts - 1
else:
return 0