📖 Scenario: Imagine you have a word, and you want to split it into parts where each part reads the same forwards and backwards. This is called palindrome partitioning. For example, the word "noon" can be split as ["n", "oo", "n"] or ["noon"].We will write a program in Go to find all such palindrome partitions of a given word using a method called backtracking.
🎯 Goal: Build a Go program that takes a string and finds all possible ways to split it into palindrome parts using backtracking.
📋 What You'll Learn
Create a string variable with the exact value "aab"
Create a helper function to check if a substring is a palindrome
Use backtracking to find all palindrome partitions of the string
Print the list of palindrome partitions in the format [["a", "a", "b"], ["aa", "b"]]
💡 Why This Matters
🌍 Real World
Palindrome partitioning is useful in text processing, DNA sequence analysis, and solving puzzles where symmetrical patterns matter.
💼 Career
Understanding backtracking and string manipulation is important for software engineers working on algorithms, coding interviews, and complex problem solving.
Progress0 / 4 steps
1
Create the input string
Create a string variable called input and set it to the exact value "aab".
DSA Go
Hint
Use input := "aab" to create the string variable.
2
Write a palindrome check function
Write a function called isPalindrome that takes a string s and returns true if s is a palindrome, otherwise false. Use a simple loop to compare characters from start and end.
DSA Go
Hint
Loop from 0 to half the string length and compare characters at positions i and len(s)-1-i.
3
Implement backtracking to find palindrome partitions
Write a function called backtrack that takes start int, input string, current []string, and result *[][]string. Use backtracking to find all palindrome partitions of input starting from start. Append partitions to result. Use isPalindrome to check substrings.
DSA Go
Hint
Use a loop from start+1 to len(input) to get substrings. If substring is palindrome, recurse with updated start and current list.
4
Print all palindrome partitions
In main, create a variable result as a slice of string slices. Call backtrack with start=0, input, empty current slice, and pointer to result. Then print result using fmt.Println(result).
DSA Go
Hint
Initialize result as var result [][]string. Call backtrack(0, input, []string{}, &result). Then print result with fmt.Println(result).
Practice
(1/5)
1. What is the main goal of palindrome partitioning using backtracking?
easy
A. To count the number of palindromes in a string
B. To find the longest palindrome substring in a string
C. To reverse a string using recursion
D. To split a string into all possible palindrome substrings
Solution
Step 1: Understand palindrome partitioning
Palindrome partitioning means dividing a string into parts where each part is a palindrome.
Step 2: Role of backtracking
Backtracking helps explore all possible ways to split the string into palindrome parts.
Final Answer:
To split a string into all possible palindrome substrings -> Option D
Quick Check:
Palindrome partitioning = all palindrome splits [OK]
Hint: Backtracking tries all splits to find palindrome partitions [OK]
Common Mistakes:
Confusing palindrome partitioning with finding longest palindrome
Thinking it only finds one partition instead of all
Mixing palindrome check with string reversal
2. Which of the following Go function signatures correctly represents a helper function for palindrome partitioning backtracking?
easy
A. func backtrack(s string, start int, path []string, result *[][]string)
B. func backtrack(s int, start string, path []int, result [][]string)
C. func backtrack(s string, path int, start []string, result [][]string)
D. func backtrack(s []string, start int, path string, result *[][]string)
Solution
Step 1: Analyze parameter types
The string to partition is 's string', start index is 'int', current path is '[]string', and result is pointer to slice of slices of strings.
Step 2: Match correct signature
func backtrack(s string, start int, path []string, result *[][]string) matches all types correctly for backtracking palindrome partitioning.
Final Answer:
func backtrack(s string, start int, path []string, result *[][]string) -> Option A
Hint: Check parameter types: string, int, slice, pointer to result [OK]
Common Mistakes:
Mixing parameter order or types
Using non-pointer for result when modification needed
Confusing path and start parameter types
3. What is the output of the following Go code snippet?
package main
import "fmt"
func isPalindrome(s string) bool {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
if s[i] != s[j] {
return false
}
}
return true
}
func backtrack(s string, start int, path []string, result *[][]string) {
if start == len(s) {
temp := make([]string, len(path))
copy(temp, path)
*result = append(*result, temp)
return
}
for end := start + 1; end <= len(s); end++ {
if isPalindrome(s[start:end]) {
backtrack(s, end, append(path, s[start:end]), result)
}
}
}
func partition(s string) [][]string {
var result [][]string
backtrack(s, 0, []string{}, &result)
return result
}
func main() {
res := partition("aab")
fmt.Println(res)
}
medium
A. [["a", "a", "b"], ["aa", "b"]]
B. [["aab"]]
C. [["a", "ab"]]
D. [["aa", "ab"]]
Solution
Step 1: Check palindrome substrings of "aab"
"a" is palindrome, "aa" is palindrome, "b" is palindrome, "ab" is not palindrome.
Step 2: Backtracking partitions
Partitions are ["a", "a", "b"] and ["aa", "b"].
Final Answer:
[["a", "a", "b"], ["aa", "b"]] -> Option A
Quick Check:
All palindrome partitions = [["a", "a", "b"], ["aa", "b"]] [OK]
Hint: Check all palindrome splits, ignore non-palindromes [OK]
Common Mistakes:
Including non-palindrome substrings like "ab"
Missing the partition with "aa"
Returning only one partition instead of all
4. Identify the bug in this Go backtracking code snippet for palindrome partitioning:
func isPalindrome(s string) bool {
for i := 0; i < len(s)/2; i++ {
if s[i] != s[len(s)-i] {
return false
}
}
return true
}
medium
A. Missing return statement
B. Incorrect loop condition, should be i <= len(s)/2
C. Index out of range error due to s[len(s)-i]
D. No bug, code is correct
Solution
Step 1: Analyze index usage in palindrome check
Index s[len(s)-i] is out of range because last index is len(s)-1, not len(s).
Step 2: Correct index to s[len(s)-1-i]
Subtract 1 from len(s) to avoid out of range error.
Final Answer:
Index out of range error due to s[len(s)-i] -> Option C
Quick Check:
Index error = Index out of range error due to s[len(s)-i] [OK]
Hint: Last index is len(s)-1, not len(s) [OK]
Common Mistakes:
Using len(s)-i instead of len(s)-1-i
Ignoring index out of range panic
Assuming loop condition fixes index error
5. Given the string "racecar", how many palindrome partitions will the backtracking algorithm find? Consider all possible partitions where each substring is a palindrome.
hard
A. 4
B. 8
C. 6
D. 10
Solution
Step 1: Identify palindrome substrings of "racecar"
Palindromes include all single characters, "cec" (indices 2-4), "aceca" (1-5), "racecar" (0-6).