-
Notifications
You must be signed in to change notification settings - Fork 4
/
String.py
executable file
·280 lines (250 loc) · 8.31 KB
/
String.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name: String
Description :
Author : amilyxy
date: 2019/9/4
-------------------------------------------------
"""
'''
28. Implement strStr(): 实现 strStr()
describe: 给定一个 haystack 字符串和一个 needle 字符串,在haystack字符串中找出needle字符串出现的第一个位置(从0开始)。
如果不存在,则返回-1。
'''
# 题解方法一 暴力滑动搜索
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
for i in range(len(haystack)-len(needle)+1):
if haystack[i: i+len(needle)] == needle:
return i
return -1
# 11.15 更新 sunday匹配方法(浩波来问的,不然我都不知道~
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
# 计算偏移表
dic = {}
n = len(needle)
n1 = len(haystack)
for i in range(n - 1, -1, -1):
if needle[i] not in dic:
dic[needle[i]] = n - i
dic['not'] = n + 1
indx = 0
while (indx + n) <= n1:
temp = haystack[indx: indx + n]
if temp == needle:
return indx
else:
if (indx + n) < n1:
if haystack[indx + n] in dic:
indx += dic[haystack[indx + n]]
else:
indx += dic['not']
else:
return -1
return indx if (indx + n) < n1 else -1
'''
14. Longest Common Prefix: 最长公共前缀
describe: 编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 ""。
'''
class Solution:
def longestCommonPrefix(self, strs: list[str]) -> str:
# 方法1 44ms
if len(strs) == 0:
return ""
L = min(map(len, strs))
j = 0
for i in range(L):
# 其实这一步相当于zip的用法
if len(set(map(lambda x: x[j], strs))) == 1:
j += 1
else:
break
return strs[0][:j]
# 方法2
strs.sort()
if len(strs) == 0: return ""
begin = strs[0]
end = strs[-1]
for i in range(len(begin)):
if begin[i] != end[i]:
return end[:i]
return begin
'''
58. Length of Last Word: 最后一个单词的长度
describe: 给定一个仅包含大小写字母和空格' '的字符串,返回其最后一个单词的长度。
如果不存在最后一个单词,请返回 0 。
'''
class Solution:
def lengthOfLastWord(self, s: str) -> int:
# 方法一
if len(s) == 0: return 0
s = s.split(' ')
for i in range(len(s)-1,-1,-1):
if s[i] != '':
return len(s[i])
if i == 0:
return 0
s = s.rstrip().split(' ') #这里split(' ')带空格很重要
return len(s[-1])
#方法二 strip()真的解决了我写了一个小时“笨方法”出现的问题 就是不知道怎么定 begin和end 太难了鸭
s = s.strip()
begin = -1
for i in range(len(s) - 1, -1, -1):
if s[i] == ' ':
begin = i
break
if begin == -1:
return len(s)
else:
return (len(s) - i - 1)
'''
387. First Unique Character in a String 字符串中的第一个唯一字符
'''
# 我的方法一
from collections import OrderedDict
class Solution:
def firstUniqChar(self, s: str) -> int:
sdict = OrderedDict()
for i in s:
sdict[i] = sdict.setdefault(i, 0) + 1
for skey, sval in sdict.items():
if sval == 1:
return s.index(skey)
return -1
# 我的方法二 (这个时间真的短!! 开心!!!
class Solution:
def firstUniqChar(self, s: str) -> int:
n = len(s)
count = set()
for i in range(n):
if s[i] not in count:
if s[i] not in s[i+1:]:
return i
else:
count.add(s[i])
return -1
# counter解法
class Solution:
def firstUniqChar(self, s: str) -> int:
from collections import Counter
for k,v in Counter(s).items():
if v==1:
return s.index(k)
return -1
# 题解方法 只需要遍历26次 @imckl
class Solution(object):
def firstUniqChar(self, s: str) -> int:
min_unique_char_index = len(s)
for c in "abcdefghijklmnopqrstuvwxyz":
i = s.find(c)
if i != -1 and i == s.rfind(c):
min_unique_char_index = min(min_unique_char_index, i)
return min_unique_char_index if min_unique_char_index != len(s) else -1
'''
383. Ransom Note 赎金信
'''
# 这个方法先把ransom和magazine遍历一次 然后再判断 有点不太高效
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
ransdict = {}
magadict = {}
for i in ransomNote:
ransdict[i] = ransdict.setdefault(i, 0) + 1
for i in magazine:
magadict[i] = magadict.setdefault(i, 0) + 1
for i, j in ransdict.items():
if i not in magadict or j > magadict[i]:
return False
return True
# 我的天哪噜 我是不是做完一轮之后对时间特别敏感... 这个时间超级短!!
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
chars = set(ransomNote)
for char in chars:
i = ransomNote.count(char)
j = magazine.count(char)
if i >j:
return False
return True
# 一行解决
# collections.Counter(magazine) & collections.Counter(ransomNote) == collections.Counter(ransomNote)
'''
344. Reverse String 反转字符串
好像题目的规范解法是双指针
'''
class Solution:
def reverseString(self, s: list[str]) -> None:
# 方法一
# s.reverse()
# 方法二 交换
n = len(s)-1
for i in range(n//2+1):
s[i], s[n-i] = s[n-i], s[i]
#方法三 不知道这个算不算原地操作
s[:] = s[::-1]
'''
151. Reverse Words in a String 反转字符串里的单词
'''
class Solution:
# 方法一
def reverseWords(self, s: str) -> str:
temp = s.split()
res = ""
for i in temp[::-1]:
res = res+i+' '
res = res.rstrip()
return res
# 方法二
def reverseWords(self, s: str) -> str:
temp = s.split()
return " ".join(temp[::-1])
# 感觉还是要用原始方法会比较好 万一面试官说让你自己实现不用高级函数 GG
def reverseWords(self, s: str) -> str:
lists = []
wordl=0
wordr=0
n = len(s)
res = ""
while wordr<n:
while wordl<n and s[wordl]==' ':
wordl+=1
# 说明找到单词左边了
wordr = wordl
while wordr<n and s[wordr]!=' ':
wordr +=1
# 说明找到单词右边了
if wordl<n:
lists.append(s[wordl: wordr])
wordl = wordr
print(lists)
for i in lists[::-1]:
res = res+i+' '
res = res.rstrip()
return res
'''
459.重复的子字符串
'''
# 方法一 暴力重复
class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
n = len(s)
for i in range(1, n):
if n%i == 0:
if s[:i]*(n//i) == s:
return True
return False
# kmp方法
class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
pnext = [0, 0]
j = 0
for i in range(1, len(s)):
while j > 0 and s[i] != s[j]:
j = pnext[j]
if s[j] == s[i]:
j += 1
pnext.append(j)
return len(s) % (len(s)-pnext[-1]) == 0 and pnext[-1] > 0