-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstringPermutation.py
More file actions
89 lines (76 loc) · 2.04 KB
/
stringPermutation.py
File metadata and controls
89 lines (76 loc) · 2.04 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
"""
BACKTRACKING TEMPLATE
def solve(c->all choices):
if is_solved(...):
return True/False or print
for c in all choices:
if is_valid(c):
update choice c
if solve(c):
return True/False
revert choice c
"""
# Write a program to print all permutations of a given string
def printString(l):
s = "".join(l)
return s
def permute(arr, l, r):
if l == r:
print(printString(arr), end=" ")
else:
for i in range(l, r+1):
arr[l], arr[i] = arr[i], arr[l]
permute(arr, l+1, r)
arr[i], arr[l] = arr[l], arr[i]
def permutations_recursion(ip, op, res, d):
if len(ip) == 0:
res.append(op)
return
d = dict()
for i in range(len(ip)):
if d.get(ip[i], None) is None:
d[ip[i]] = 0
newIp = ip[:i] + ip[i+1:]
newOp = op + [ip[i]]
permutations_recursion(newIp, newOp, res, d)
def swap(a, i, j):
return a[:i]+a[j]+a[i+1:j]+a[i]+a[j+1:]
def findMaximumNum(num, k, maxm):
# base condition
if k == 0:
return
lNum = len(num)
for i in range(lNum-1):
for j in range(i+1, lNum):
if num[i] < num[j] and num[j] >= max(num[j:]):
num = swap(num, i, j)
if maxm[0] < num:
maxm[0] = num
findMaximumNum(num, k-1, maxm)
num = swap(num, i, j)
# https://leetcode.com/problems/subsets
def subsets(nums, start, end, tmp, res):
res.append(tmp[:])
for i in range(start, end):
tmp.append(nums[i])
subsets(nums, i+1, end, tmp, res)
tmp.pop()
if __name__ == "__main__":
s = "abc"
l = list(s)
lens = len(s)
permute(l, 0, lens-1)
print()
a = [1,1,2]
res = list()
permutations_recursion(a, [], res, dict())
print(res)
num = "4577"
k = 4
maxm = ["0"]
findMaximumNum(num, k, maxm)
print(maxm[0])
nums = [1,2,3]
res = []
subsets(nums, 0, len(nums), [], res)
print(res)