forked from Srinivas11789/AlgorithmNuggets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.py
More file actions
66 lines (55 loc) · 1.78 KB
/
sort.py
File metadata and controls
66 lines (55 loc) · 1.78 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
class Solution(object):
def customSortString(self, S, T):
"""
:type S: str
:type T: str
:rtype: str
"""
# Use a differernt/custom key for the sorted function with reference to the given array S
# 100 pass
def keys(char):
if char in S:
return S.index(char)
else:
# Return anything higher will suggest the sorted function to place at the end - 1000, or len(T) or 26
return len(T)
return "".join(sorted(T, key=keys))
"""
# Construct a sorted alp with Only lower case characters then solve
key = ["a"]
for i in range(25):
key.append(chr(ord(key[-1])+1))
start = key.index(S[0])
key = key[start:]+key[:start]
return sorted(T,key=key)
"""
"""
# Logic 1 - Any combination - so add the array S in the same order with others
result = []
match = []
for char in T:
if char not in S:
result.append(char)
else:
match.append(char)
i = 0
S = list(S)
while i < len(S):
if S[i] not in match:
del S[i]
elif match.count(S[i]) > 1:
S = S[:i]+[S[i]]*(match.count(S[i])-1)+S[i:]
i += match.count(S[i])-1
else:
i += 1
return "".join(result)+"".join(S)
"""
"""
# One Logic of iteration
result = []
for char in T:
if char in S:
for char in result:
else:
result.append(char)
"""