- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray1.py
More file actions
Latest commit
214 lines (185 loc) · 6.01 KB
/
Copy patharray1.py
File metadata and controls
214 lines (185 loc) · 6.01 KB
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
importmath
deffruits_into_baskets(fruits):
K=2
iflen(fruits) <1:
return-1
start=0
max_l=0
dict_fruits= {}
foriinrange(len(fruits)):
c=fruits[i]
ifcnotindict_fruits:
dict_fruits[c] =0
dict_fruits[c] +=1
whilelen(dict_fruits) >K:
l=fruits[start]
dict_fruits[l] -=1
ifdict_fruits[l] ==0:
deldict_fruits[l]
start+=1
max_l=max(max_l, i-start+1)
returnmax_l
defnon_repeat_substring(str):
iflen(str) <1:
return-1
max_l=0
start=0
dict_set= {}
foriinrange(len(str)):
c=str[i]
ifcindict_set:
start=max(max_l, dict_set[c] +1)
dict_set[c] =i
max_l=max(max_l, i-start+1)
returnmax_l
deflength_of_longest_substring(str, k):
# TODO: Write your code here
iflen(str) <1:
return-1
start, max_l, max_letter_count=0,0,0
dict_map= {}
foriinrange(len(str)):
l=str[i]
iflnotindict_map:
dict_map[l] =0
dict_map[l] +=1
max_letter_count=max(max_letter_count, dict_map[l])
if(i-start+1-max_letter_count) >k:
f=str[start]
dict_map[f] -=1
start+=1
max_l=max(max_l, i-start+1)
returnmax_l
deflength_of_longest_substring1(arr, k):
start, max_l, max_one_count=0,0,0
foriinrange(len(arr)):
ifarr[i] ==1:
max_one_count+=1
ifi-start+1-max_one_count>k:
ifarr[start] ==1:
max_one_count-=1
start+=1
max_l=max(max_l, i-start+1)
returnmax_l
deftriplet_sum_close_to_target(arr, target_sum):
smallest=math.inf
arr.sort()
foriinrange(len(arr) -2):
left=i+1
right=len(arr) -1
whileleft<right:
t_diff=target_sum-arr[i] -arr[left] -arr[right]
ift_diff==0:
returntarget_sum-t_diff
ifabs(t_diff) <abs(smallest):
smallest=t_diff
ift_diff>0:
left+=1
else:
right-=1
returntarget_sum-smallest
deftriplet_with_smaller_sum(arr, target):
arr.sort()
count=0
foriinrange(len(arr) -2):
count+=searchPair(arr, target-arr[i], i)
returncount
defsearchPair(arr, target_sum, first):
left=first+1
right=len(arr) -1
count=0
whileleft<right:
ifarr[left] +arr[right] <target_sum:
count+=right-left
left+=1
else:
right=-1
returncount
deffind_permutation(str, pattern):
window_start, matched=0, 0
char_frequency= {}
forchrinpattern:
ifchrnotinchar_frequency:
char_frequency[chr] =0
char_frequency[chr] +=1
# our goal is to match all the characters from the 'char_frequency' with the current window
# try to extend the range [window_start, window_end]
forwindow_endinrange(len(str)):
right_char=str[window_end]
ifright_charinchar_frequency:
# decrement the frequency of matched character
char_frequency[right_char] -=1
ifchar_frequency[right_char] ==0:
matched+=1
ifmatched==len(char_frequency):
returnTrue
# shrink the window by one character
ifwindow_end>=len(pattern) -1:
left_char=str[window_start]
window_start+=1
ifleft_charinchar_frequency:
ifchar_frequency[left_char] ==0:
matched-=1
char_frequency[left_char] +=1
returnFalse
deffind_string_anagrams(str, pattern):
result_indexes= []
match=0
start=0
dict_map= {}
forpinpattern:
ifpnotindict_map:
dict_map[p] =0
dict_map[p] +=1
foriinrange(len(str)):
l=str[i]
iflindict_map:
dict_map[l] -=1
ifdict_map[l] ==0:
match+=1
ifmatch==len(pattern):
result_indexes.append(start)
ifi>=len(pattern) -1:
c=str[start]
start+=1
ifcindict_map:
ifdict_map[c] ==0:
match-=1
dict_map[c] +=1
returnresult_indexes
deffind_permutation2(str, pattern):
window_start, matched, substr_start=0, 0, 0
min_length=len(str) +1
char_frequency= {}
forchrinpattern:
ifchrnotinchar_frequency:
char_frequency[chr] =0
char_frequency[chr] +=1
# try to extend the range [window_start, window_end]
forwindow_endinrange(len(str)):
right_char=str[window_end]
ifright_charinchar_frequency:
char_frequency[right_char] -=1
ifchar_frequency[right_char] >=0: # Count every matching of a character
matched+=1
# Shrink the window if we can, finish as soon as we remove a matched character
whilematched==len(pattern):
ifmin_length>window_end-window_start+1:
min_length=window_end-window_start+1
substr_start=window_start
left_char=str[window_start]
window_start+=1
ifleft_charinchar_frequency:
# Note that we could have redundant matching characters, therefore we'll decrement the
# matched count only when the last occurrence of a matched character is going out of the window
ifchar_frequency[left_char] ==0:
matched-=1
char_frequency[left_char] +=1
ifmin_length>len(str):
return""
returnstr[substr_start:substr_start+min_length]
#print("Maximum number of fruits: " + str(fruits_into_baskets(['A', 'B', 'C', 'A', 'C'])))
#print("Length of the longest substring: " + str(non_repeat_substring("aabccbb")))
#print('Permutation exist: ' + str(find_permutation("aaacb", "abc")))
#print(find_string_anagrams("ppqp", "pq"))
print(find_permutation2("aabdec", "abc"))