forked from super30admin/Greedy-4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimum_path.py
39 lines (37 loc) · 1.25 KB
/
minimum_path.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
class Solution:
def shortestWay(self, source: str, target: str) -> int:
##time - O(nm)
##space-O(1)
######################### sol1-same impl #################
i = 0
j = temp = ro = 0
# while i<len(target) and j<len(source):
# if target[i]==source[j]:
# recur=0
# i+=1
# j+=1
# else:
# j+=1
# # print(ro)
# if j==len(source) and recur>0:
# return -1
# elif i==len(target) and j!=len(source):
# return ro+1
# elif j==len(source):
# j=0
# recur+=1
# ro+=1
# return ro
######################### sol2-same impl #################
count = 0
while j < len(target):
count += 1
i = 0
curr = j
while i < len(source) and j < len(target):
if target[j] == source[i]:
j += 1
i += 1
if curr == j:
return -1
return count