File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments