forked from calistus-igwilo/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.py
412 lines (353 loc) · 10.9 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
"""
You are given a string consisting of only lowercase and uppercase English Alphabets
and integers 0 to 9. Write a function that will take this string as input and return
the index of the first character that is repeating.
"""
from posixpath import split
from turtle import clear
def repeat(s):
hashTable = {}
for i in range(len(s)-1):
if s[i] in hashTable:
return i
else:
hashTable[s[i]] = i
return []
s = "abcdeefgghhij"
print(repeat(s))
"""
You are given a string consisting of only lowercase and uppercase English Alphabets
and integers 0 to 9. Write a function that will take this string as input and return
the index of the first character that is non-repeating.
"""
# Method 1:
def non_repeat(s):
repeats = []
for i in range(len(s)-1):
if s[i] in s[i+1:]:
repeats.append(s[i])
for i in range(len(s)):
if s[i] not in repeats:
return i
return None
# Time complexity = O(n). Space complexity = O(n)
s = "abaccbd"
print(non_repeat(s))
# Method 2: Improved brute force
def non_repeat(s):
for i in range(len(s)):
repeat = False
for j in range(len(s)):
if s[i] == s[j] and i != j:
repeat = True
if repeat == False:
return i
return None
# Time complexity = O(n**2). Space complexity = O(1)
print(non_repeat(s))
# Method 3 Use of Hash Table
def non_repeat(s):
hashTable = {}
for i in s:
if i in hashTable:
hashTable[i] += 1
else:
hashTable[i] = 1
for i in range(len(s)):
if hashTable[s[i]] == 1:
return i
return None
# Time complexity = O(n).
# Space complexity = O(1) because s has a fixed value (26 upper + 26 lower + 10 digits)
print(non_repeat(s))
"""
You are given a string. Write a function to check whether the string is a
palindrome or not.
"""
def palindrome(s):
str_ = ""
for i in range(len(s)-1, -1, -1):
str_ += s[i]
print(str_)
if str_ == s:
return True
return False
# Time complexity = O(n**2) because strings are immutable, so append creates a
# new string for each call.
# Space complexity = O(n)
s = "abba"
print(palindrome(s))
# Method 2: Appending to an array
def palindrome(s):
arr = []
for i in range(len(s)-1, -1, -1):
arr.append(s[i])
result = ''.join(arr)
if s == result:
return True
return False
# Time complexity = O(n) because append to array is a constant time operation
# and the array was traversed once.
# Space complexity = O(n) as extra sapce was created to store the new array
print(palindrome(s))
# Method 3
def palindrome(s):
i = 0
j = len(s)-1
while i <= j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
# Time complexity = O(n)
# Space complexity = O(1) since no extra space was created
print(palindrome(s))
"""
Given a string s, find the length of the longest substring without repeating characters
"""
def max_substr(s):
strList = []
hashTable = {}
max_ = 0
start = 0
for i in range(len(s)-1):
if s[i] not in hashTable:
hashTable[s[i]] = i
max_ = max(start, i - start+1)
else:
strList.append(s[start:i])
start = max(start, hashTable[s[i]]+1)
hashTable[s[i]] = i
print(f'Substrings: {strList}')
return max_
# Time complexity = O(n). Space complexity = O(n)
s = 'abcdbefghijklmef'
print(max_substr(s))
"""
Given an array of strings consisting of lower case English letters, group the
anagrams together. You can return the answer in any order. An Anagram is a
word or phrase formed by rearranging the letters of a different word or phrase,
using all the original letters exactly once.
"""
def anagrams(arr):
list_ = []
hashTable = {}
if len(arr) == 1 or len(arr) == 0:
return arr
print(arr)
sortList = [''.join(sorted(item)) for item in arr]
for i in range(len(arr)):
if sortList[i] in hashTable:
hashTable[sortList[i]] += [arr[i]]
else:
hashTable[sortList[i]] = [arr[i]]
return hashTable.values()
# Time complexity = O(S x nlogn) where S = number of strings and n = max number of
# strings in the character.
# Space complexity = O(S x n)
arr = ['arc', 'abc', 'car', 'cat', 'act', 'acb', 'atc']
print(anagrams(arr))
"""
Given an array of integers which is sorted in ascending order, and a target integer,
write a function to search for whether the target integer is there in the given array.
If it is there, then return its index. Otherwise, return -1. You must write an
algorithm with O(logn) runtime complexity.
"""
def binary_search(nums, target):
pointerLeft = 0
pointerRight = len(nums)-1
while pointerLeft <= pointerRight:
middle = (pointerRight + pointerLeft) // 2
print(f'pointerLeft: {pointerLeft}, pointerRight: {pointerRight}')
if target == nums[middle]:
return middle
elif target < nums[middle]:
pointerRight = middle - 1
elif target > nums[middle]:
pointerLeft = middle + 1
return -1
nums = [2, 3, 7, 9, 11, 23, 37, 81, 87, 89]
print(binary_search(nums, 9))
"""
You are given an integer array nums sorted in ascending order (with disctinct values).
Prior to being passed to your function, nums is possibly rotated at an unkown pivot
index k (1 <= k < nums.lenght) such that the resulting array is
[nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed).
For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].
Given an integer target, return the index of target if it is in the array, else return
-1. You must write an alogrithm with O(logn) runtime complexity.
"""
def search_rotation(nums, target):
left = 0
right = len(nums)-1
while left <= right:
middle = (left + right) // 2
if nums[middle] == target:
return middle
if nums[left] < nums[middle]:
if nums[left] <= target and target < nums[middle]:
right = middle-1
else:
left = middle+1
else:
if nums[middle] < target and target <= nums[right]:
left = middle+1
else:
right = middle-1
return -1
nums = [5,6,7,8,9,1,2,3,4]
target = 3
print(search_rotation(nums, target))
# Compress the string
"""
You are given a string S Suppose a character 'c' occurs consecutively X
times in the string. Replace these consecutive occurrences of the
character 'c' with (X, c) in the string.
"""
S = 1222311
def compress_string(S):
result = []
temp = []
pointer = 1
i = 0
j = i+1
cnt = 1
if len(S) == 0:
return "string must contain at least 1 element"
while S(j):
temp.append(S[i])
while S[i] == S[j]:
cnt += 1
i += 1
result.append((cnt, temp[-1]))
temp = []
print(result)
"""
Kevin and Stuart want to play the 'The Minion Game'.
Game Rules
Both players are given the same string, .
Both players have to make substrings using the letters of the string .
Stuart has to make words starting with consonants.
Kevin has to make words starting with vowels.
The game ends when both players have made all possible substrings.
Scoring
A player gets +1 point for each occurrence of the substring in the string .
"""
def minion_game(string_):
vowel_strings = 0
consonant_strings = 0
string_length = len(string_) # length of string
for i in range(string_length):
if string_[i] in "AEIOU":
vowel_strings += string_length - i # the number of substrings that can be formed from the length
else:
consonant_strings += string_length -i # the number of substrings that can be formed
# Determine the winner
if consonant_strings > vowel_strings:
print("Stuart", consonant_strings )
elif consonant_strings < vowel_strings:
print("Kevin", vowel_strings)
else:
print("Draw")
minion_game("BANANA")
def merge_the_tools(string, k):
sub_sequences = []
temp = ""
for i in range(0, len(string), k):
sub_sequences.append((string[i:i+k]))
print(sub_sequences)
print("lenght of subsequence: ",len(sub_sequences))
# check for duplicate alphabets
for string in sub_sequences:
for i in range(k):
if string[i] not in temp:
temp += string[i]
print(temp)
temp = ""
s = 'AAABCADDE'
merge_the_tools(s)
"""
You are given a string and your task is to swap cases. In other words, convert all
lowercase letters to uppercase letters and vice versa.
"""
def swap_case(s):
result = ""
for i in range(len(s)):
if s[i].isupper():
result += s[i].lower()
else:
result += s[i].upper()
return result
t = "Www.HackerRank.com"
swap_case(t)
"""
A user enters a string and a substring. You have to print the number of times that the
substring occurs in the given string. String traversal will take place from left to right,
not from right to left.
String letters are case-sensitive.
"""
def count_substring(string, sub_string):
substring_length = len(sub_string)
string_length = len(string)
end = string_length - 2
cnt = 0
for i in range(0, end):
if string[i:i+substring_length] == sub_string:
cnt += 1
return cnt
s = "ABCDCDC"
sub = "CDC"
count_substring(s, sub) # 2
"""
You are given a string S.
Your task is to find out if the string contains: alphanumeric characters,
alphabetical characters, digits, lowercase and uppercase characters.
Output Format:
In the first line, print True if has any alphanumeric characters. Otherwise, print False.
In the second line, print True if has any alphabetical characters. Otherwise, print False.
In the third line, print True if has any digits. Otherwise, print False.
In the fourth line, print True if has any lowercase characters. Otherwise, print False.
In the fifth line, print True if has any uppercase characters. Otherwise, print False.
"""
s = "qA2"
print(s.isalnum())
def string_type(s):
temp = []
for char in s:
temp.append(char.isalnum())
if True in temp:
print(True)
else:
print(False)
temp =[]
for char in s:
temp.append(char.isalpha())
if True in temp:
print(True)
else:
print(False)
temp =[]
for char in s:
temp.append(char.isdigit())
if True in temp:
print(True)
else:
print(False)
temp =[]
for char in s:
temp.append(char.islower())
if True in temp:
print(True)
else:
print(False)
temp =[]
for char in s:
temp.append(char.isupper())
if True in temp:
print(True)
else:
print(False)
temp =[]
z = "qA2"
string_type(z)