Skip to content

Commit ddf9679

Browse files
Merge pull request #616 from hack-to-fest/master
palindrome code
2 parents acd19dc + 1962de1 commit ddf9679

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

Python/palindrom/palindrom.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
def checkPalindrome(string):
2+
3+
# Returns true if str is palindrome,
4+
# else false
5+
length = len(string)
6+
length -= 1
7+
for i in range(length):
8+
if string[i] != string[length]:
9+
return False
10+
length -= 1
11+
return True
12+
13+
def printSolution(partitions):
14+
for i in range(len(partitions)):
15+
for j in range(len(partitions[i])):
16+
print(partitions[i][j], end = " ")
17+
print()
18+
19+
def addStrings(v, s, temp, index):
20+
21+
# Goes through all indexes and
22+
# recursively add remaining partitions
23+
# if current string is palindrome.
24+
length = len(s)
25+
string = ""
26+
27+
current = temp[:]
28+
29+
if index == 0:
30+
temp = []
31+
for i in range(index, length):
32+
string += s[i]
33+
if checkPalindrome(string):
34+
temp.append(string)
35+
if i + 1 < length:
36+
addStrings(v, s, temp[:], i + 1)
37+
else:
38+
v.append(temp)
39+
temp = current
40+
41+
def partition(s, v):
42+
43+
# Generates all palindromic partitions
44+
# of 's' and stores the result in 'v'.
45+
temp = []
46+
addStrings(v, s, temp[:], 0)
47+
printSolution(v)
48+
49+
# Driver Code
50+
if __name__ == "__main__":
51+
s = "geeks"
52+
partitions = []
53+
partition(s, partitions)
54+

0 commit comments

Comments
 (0)