-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindAnagrams.py
48 lines (39 loc) · 1.33 KB
/
findAnagrams.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
import math
def findAnagrams (word, dict) :
chars = list(word)
# starting with the first in lexi order
chars.sort()
# factional permutations to be generated
for i in range(math.factorial(len(chars))) :
''' find the next in order by finding the biggest char
that is less than the char on its right, swap these chars,
then reverse the chars behind these - 14th Century algorithm :O
'''
string = ''.join(chars)
#if string in dict :
print(string)
k = 0
l = 0
# find largest k where chars[k] < chars[k+1]
for i in range(len(chars)-2, -1, -1) :
if chars[i] < chars[i+1] :
k = i
for j in range(i+1, len(chars)) :
if chars[i] < chars[j] :
l = j
break;
# swap k and k+1 elements
temp = chars[k]
chars[k] = chars[l]
chars[l] = temp
# reverse all elements to the right of k (decending -> ascending)
i = k + 1
j = len(chars) - 1
while i < j :
temp = chars[i]
chars[i] = chars[j]
chars[j] = temp
i += 1
j -= 1
dict = set(['dog', 'god'])
findAnagrams('123', dict)