From 4bdaa5cff51ba37c8964dc18cdfc93a9aaa98a7c Mon Sep 17 00:00:00 2001 From: Nobert Patrick Date: Sat, 25 Nov 2023 00:28:10 +0300 Subject: [PATCH 1/4] Implemented Palindrome Partitioning using Backtracking algorithm --- Backtracking/PalindromePartitioning.js | 46 +++++++++++++++++++ .../tests/PalindromePartitioning.test.js | 29 ++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 Backtracking/PalindromePartitioning.js create mode 100644 Backtracking/tests/PalindromePartitioning.test.js diff --git a/Backtracking/PalindromePartitioning.js b/Backtracking/PalindromePartitioning.js new file mode 100644 index 0000000000..8726310abe --- /dev/null +++ b/Backtracking/PalindromePartitioning.js @@ -0,0 +1,46 @@ +/* + * 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 diff --git a/Backtracking/tests/PalindromePartitioning.test.js b/Backtracking/tests/PalindromePartitioning.test.js new file mode 100644 index 0000000000..12191e14eb --- /dev/null +++ b/Backtracking/tests/PalindromePartitioning.test.js @@ -0,0 +1,29 @@ +import PalindromePartitioning from '../PalindromePartitioning' + +describe('PalindromePartitioning', () => { + it('should partition a string into palindromes', () => { + const pp = new PalindromePartitioning() + const result = pp.partition('aab') + + expect(result).toEqual( + expect.arrayContaining([ + ['a', 'a', 'b'], + ['aa', 'b'] + ]) + ) + }) + + it('should handle empty string', () => { + const pp = new PalindromePartitioning() + const result = pp.partition('') + + expect(result).toEqual([[]]) + }) + + it('should handle a single character string', () => { + const pp = new PalindromePartitioning() + const result = pp.partition('c') + + expect(result).toEqual([['c']]) + }) +}) From cb53c8c65a02d10096426cc2c2aa1ca7b981d971 Mon Sep 17 00:00:00 2001 From: Nobert Patrick Date: Sat, 25 Nov 2023 11:57:25 +0300 Subject: [PATCH 2/4] fix:Updated palindromePartition algorithm --- Backtracking/PalindromePartitioning.js | 46 ------------ .../tests/PalindromePartitioning.test.js | 29 -------- Recursive/PalindromePartitioning.js | 74 +++++++++++++++++++ Recursive/test/PalindromePartitioning.test.js | 12 +++ 4 files changed, 86 insertions(+), 75 deletions(-) delete mode 100644 Backtracking/PalindromePartitioning.js delete mode 100644 Backtracking/tests/PalindromePartitioning.test.js create mode 100644 Recursive/PalindromePartitioning.js create mode 100644 Recursive/test/PalindromePartitioning.test.js diff --git a/Backtracking/PalindromePartitioning.js b/Backtracking/PalindromePartitioning.js deleted file mode 100644 index 8726310abe..0000000000 --- a/Backtracking/PalindromePartitioning.js +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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 diff --git a/Backtracking/tests/PalindromePartitioning.test.js b/Backtracking/tests/PalindromePartitioning.test.js deleted file mode 100644 index 12191e14eb..0000000000 --- a/Backtracking/tests/PalindromePartitioning.test.js +++ /dev/null @@ -1,29 +0,0 @@ -import PalindromePartitioning from '../PalindromePartitioning' - -describe('PalindromePartitioning', () => { - it('should partition a string into palindromes', () => { - const pp = new PalindromePartitioning() - const result = pp.partition('aab') - - expect(result).toEqual( - expect.arrayContaining([ - ['a', 'a', 'b'], - ['aa', 'b'] - ]) - ) - }) - - it('should handle empty string', () => { - const pp = new PalindromePartitioning() - const result = pp.partition('') - - expect(result).toEqual([[]]) - }) - - it('should handle a single character string', () => { - const pp = new PalindromePartitioning() - const result = pp.partition('c') - - expect(result).toEqual([['c']]) - }) -}) diff --git a/Recursive/PalindromePartitioning.js b/Recursive/PalindromePartitioning.js new file mode 100644 index 0000000000..8ab859d77c --- /dev/null +++ b/Recursive/PalindromePartitioning.js @@ -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 diff --git a/Recursive/test/PalindromePartitioning.test.js b/Recursive/test/PalindromePartitioning.test.js new file mode 100644 index 0000000000..1bb218cdd4 --- /dev/null +++ b/Recursive/test/PalindromePartitioning.test.js @@ -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']]) + }) +}) From ac42c95e5561279c6e1c9b03099e574c3f499cef Mon Sep 17 00:00:00 2001 From: Nobert Patrick Date: Sat, 25 Nov 2023 12:03:03 +0300 Subject: [PATCH 3/4] code clean up --- Recursive/PalindromePartitioning.js | 41 ----------------------------- 1 file changed, 41 deletions(-) diff --git a/Recursive/PalindromePartitioning.js b/Recursive/PalindromePartitioning.js index 8ab859d77c..701036ecf5 100644 --- a/Recursive/PalindromePartitioning.js +++ b/Recursive/PalindromePartitioning.js @@ -6,47 +6,6 @@ * - 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) => { From 66305adca8c3a3b65f8bc4a3a6e93040ab5ed45a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Sat, 25 Nov 2023 18:55:02 +0100 Subject: [PATCH 4/4] Rephrase doc comment & move to appropriate function --- Recursive/PalindromePartitioning.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/Recursive/PalindromePartitioning.js b/Recursive/PalindromePartitioning.js index 701036ecf5..9a5150f65d 100644 --- a/Recursive/PalindromePartitioning.js +++ b/Recursive/PalindromePartitioning.js @@ -1,13 +1,10 @@ -/* - * 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 - */ - import { palindrome } from './Palindrome' +/* + * Given a string s, return all possible palindrome partitionings of s. + * A palindrome partitioning partitions a string into palindromic substrings. + * @see https://www.cs.columbia.edu/~sedwards/classes/2021/4995-fall/proposals/Palindrome.pdf + */ const partitionPalindrome = (s) => { const result = [] backtrack(s, [], result)