-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsol.py
24 lines (22 loc) · 822 Bytes
/
sol.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
def shortestSubstring(s, chars):
if not s or not chars:
return None
left = 0
right = 0
found = False
while right < len(s):
while right < len(s) and not all(char in s[left:right] for char in chars):
right+=1
while all(char in s[left:right] for char in chars):
found = True
left+=1
sub = s[left-1:right]
if not found:
return "characters from set not present in string"
return sub
if __name__=='__main__':
print(shortestSubstring(s="figehaeci", chars={'a', 'e', 'i'}))
print(shortestSubstring(s="figehaieci", chars={'a', 'e', 'i'}))
print(shortestSubstring(s="figehaieci", chars={'a', 'z'}))
print(shortestSubstring(s="", chars={'a', 'z'}))
print(shortestSubstring(s="figehaieci", chars=set()))