0
0
DSA Typescriptprogramming~20 mins

Palindrome Partitioning Using Backtracking in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Palindrome Partitioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Palindrome Partitioning for 'aab'
What is the output of the following TypeScript code that finds all palindrome partitions of the string 'aab'?
DSA Typescript
function isPalindrome(s: string, left: number, right: number): boolean {
  while (left < right) {
    if (s[left] !== s[right]) return false;
    left++;
    right--;
  }
  return true;
}

function backtrack(s: string, start: number, path: string[], result: string[][]): void {
  if (start === s.length) {
    result.push([...path]);
    return;
  }
  for (let end = start; end < s.length; end++) {
    if (isPalindrome(s, start, end)) {
      path.push(s.substring(start, end + 1));
      backtrack(s, end + 1, path, result);
      path.pop();
    }
  }
}

function partition(s: string): string[][] {
  const result: string[][] = [];
  backtrack(s, 0, [], result);
  return result;
}

console.log(partition('aab'));
A[["a","a","b"],["aa","b"]]
B[["aab"]]
C[["a","ab"],["aa","b"]]
D[["a","a"],["b"]]
Attempts:
2 left
💡 Hint
Think about all ways to split the string where each part is a palindrome.
🧠 Conceptual
intermediate
1:30remaining
Number of Palindrome Partitions for 'racecar'
How many palindrome partitions does the string 'racecar' have using backtracking?
A4
B10
C6
D12
Attempts:
2 left
💡 Hint
Consider all ways to split 'racecar' into palindromic substrings.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Palindrome Partitioning Code
What error does the following TypeScript code produce when run with input 'abc'?
DSA Typescript
function isPalindrome(s: string, left: number, right: number): boolean {
  while (left <= right) {
    if (s[left] !== s[right]) return false;
    left++;
    right--;
  }
  return true;
}

function backtrack(s: string, start: number, path: string[], result: string[][]): void {
  if (start === s.length) {
    result.push([...path]);
    return;
  }
  for (let end = start; end < s.length; end++) {
    if (isPalindrome(s, start, end)) {
      path.push(s.substring(start, end + 1));
      backtrack(s, end + 1, path, result);
      path.pop();
    }
  }
}

function partition(s: string): string[][] {
  const result: string[][] = [];
  backtrack(s, 0, [], result);
  return result;
}

console.log(partition('abc'));
ASyntaxError: Unexpected token
BNo error, outputs [['a','b','c']]
CTypeError: s.substring is not a function
DRangeError: Maximum call stack size exceeded
Attempts:
2 left
💡 Hint
Check the palindrome check condition carefully.
Predict Output
advanced
2:00remaining
Output of Palindrome Partitioning for 'aaa'
What is the output of the following TypeScript code that partitions 'aaa' into palindromes?
DSA Typescript
function isPalindrome(s: string, left: number, right: number): boolean {
  while (left < right) {
    if (s[left] !== s[right]) return false;
    left++;
    right--;
  }
  return true;
}

function backtrack(s: string, start: number, path: string[], result: string[][]): void {
  if (start === s.length) {
    result.push([...path]);
    return;
  }
  for (let end = start; end < s.length; end++) {
    if (isPalindrome(s, start, end)) {
      path.push(s.substring(start, end + 1));
      backtrack(s, end + 1, path, result);
      path.pop();
    }
  }
}

function partition(s: string): string[][] {
  const result: string[][] = [];
  backtrack(s, 0, [], result);
  return result;
}

console.log(partition('aaa'));
A[["a","a","a"]]
B[["aaa"]]
C[["a","a","a"],["a","aa"],["aa","a"],["aaa"]]
D[["a","aa"]]
Attempts:
2 left
💡 Hint
Consider all palindrome substrings including overlapping ones.
🧠 Conceptual
expert
1:30remaining
Time Complexity of Palindrome Partitioning Using Backtracking
What is the time complexity of the palindrome partitioning algorithm using backtracking for a string of length n?
AO(2^n)
BO(n^3)
CO(n!)
DO(n * 2^n)
Attempts:
2 left
💡 Hint
Consider the number of partitions and palindrome checks.