Skip to content

palindrome code #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions palindrom/palindrom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
def checkPalindrome(string):

# Returns true if str is palindrome,
# else false
length = len(string)
length -= 1
for i in range(length):
if string[i] != string[length]:
return False
length -= 1
return True

def printSolution(partitions):
for i in range(len(partitions)):
for j in range(len(partitions[i])):
print(partitions[i][j], end = " ")
print()

def addStrings(v, s, temp, index):

# Goes through all indexes and
# recursively add remaining partitions
# if current string is palindrome.
length = len(s)
string = ""

current = temp[:]

if index == 0:
temp = []
for i in range(index, length):
string += s[i]
if checkPalindrome(string):
temp.append(string)
if i + 1 < length:
addStrings(v, s, temp[:], i + 1)
else:
v.append(temp)
temp = current

def partition(s, v):

# Generates all palindromic partitions
# of 's' and stores the result in 'v'.
temp = []
addStrings(v, s, temp[:], 0)
printSolution(v)

# Driver Code
if __name__ == "__main__":
s = "geeks"
partitions = []
partition(s, partitions)