Leetcode Problem 1147. Longest Chunked Palindrome Decomposition

1147. Longest Chunked Palindrome Decomposition

Leetcode Solutions

Two Pointers Greedy Approach

  1. Initialize two pointers, left and right, pointing to the start and end of the string respectively.
  2. Initialize two temporary strings, temp1 and temp2, to store the characters being compared.
  3. Initialize a counter count to keep track of the number of valid decompositions.
  4. While left is less than right: a. Append the character at left to temp1 and the character at right to temp2. b. If temp1 is equal to the reverse of temp2, a valid decomposition is found: i. Increment count by 2. ii. Reset temp1 and temp2 to empty strings. iii. Move left and right pointers inward. c. If no match is found, continue moving the pointers inward.
  5. After the loop, if left equals right and temp1 is empty, increment count by 1.
  6. If temp1 is not empty, increment count by 1.
  7. Return the value of count.
UML Thumbnail

Recursive Decomposition Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...