Skip to content

Implemented Palindrome Partitioning using Backtracking algorithm #1591

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

Merged
merged 4 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
74 changes: 74 additions & 0 deletions Recursive/PalindromePartitioning.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Problem Statement: Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.
* what is palindrome partitioning?
* - Palindrome partitioning means, partitioning a string into substrings such that every substring is a palindrome.
* Reference to know more about palindrome partitioning:
* - https://www.cs.columbia.edu/~sedwards/classes/2021/4995-fall/proposals/Palindrome.pdf
*/

// class PalindromePartitioning {
// partition(s) {
// const result = []
// this.backtrack(s, [], result)
// return result
// }

// backtrack(s, path, result) {
// if (s.length === 0) {
// result.push([...path])
// return
// }

// for (let i = 0; i < s.length; i++) {
// const prefix = s.substring(0, i + 1)
// if (this.isPalindrome(prefix)) {
// path.push(prefix)
// this.backtrack(s.substring(i + 1), path, result)
// path.pop()
// }
// }
// }

// isPalindrome(s) {
// let start = 0
// let end = s.length - 1
// while (start < end) {
// if (s.charAt(start) !== s.charAt(end)) {
// return false
// }
// start++
// end--
// }
// return true
// }
// }

// export default PalindromePartitioning

// use a function instead of class and reuse existing palindrome function not isPalindrome function

import { palindrome } from './Palindrome'

const partitionPalindrome = (s) => {
const result = []
backtrack(s, [], result)
return result
}

const backtrack = (s, path, result) => {
if (s.length === 0) {
result.push([...path])
return
}

for (let i = 0; i < s.length; i++) {
const prefix = s.substring(0, i + 1)
if (palindrome(prefix)) {
path.push(prefix)
backtrack(s.substring(i + 1), path, result)
path.pop()
}
}
}

export default partitionPalindrome
12 changes: 12 additions & 0 deletions Recursive/test/PalindromePartitioning.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import partitionPalindrome from '../PalindromePartitioning'

describe('Palindrome Partitioning', () => {
it('should return all possible palindrome partitioning of s', () => {
expect(partitionPalindrome('aab')).toEqual([
['a', 'a', 'b'],
['aa', 'b']
])
expect(partitionPalindrome('a')).toEqual([['a']])
expect(partitionPalindrome('ab')).toEqual([['a', 'b']])
})
})