Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if a substring is a palindrome.
DSA Rust
fn is_palindrome(s: &str) -> bool {
let chars: Vec<char> = s.chars().collect();
let mut left = 0;
let mut right = chars.len() - [1];
while left < right {
if chars[left] != chars[right] {
return false;
}
left += 1;
right -= 1;
}
true
}Attempts:
3 left
💡 Hint
Common Mistakes
Using length directly as index causes out-of-bounds error.
Starting right pointer at 0 instead of last index.
✗ Incorrect
We subtract 1 from the length to get the last index because indexing starts at 0.
2fill in blank
mediumComplete the code to push a substring slice into the current partition vector.
DSA Rust
fn backtrack(s: &str, start: usize, current: &mut Vec<String>, result: &mut Vec<Vec<String>>) {
if start == s.len() {
result.push(current.clone());
return;
}
for end in start + 1..=s.len() {
let substring = &s[start..end];
if is_palindrome(substring) {
current.push(substring.to_string());
backtrack(s, end, current, result);
current.[1]();
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using clear() removes all elements, not just the last.
Using push() instead of removing causes infinite growth.
✗ Incorrect
We use pop() to remove the last added substring after backtracking.
3fill in blank
hardFix the error in the for loop range to avoid panic when slicing the string.
DSA Rust
for end in start + 1..[1] { let substring = &s[start..end]; if is_palindrome(substring) { current.push(substring.to_string()); backtrack(s, end, current, result); current.pop(); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using s.len() causes the last character to be excluded.
Using start as end causes empty slices.
✗ Incorrect
The range must go up to s.len() + 1 to include the last character in slicing.
4fill in blank
hardFill both blanks to create a recursive backtracking function that partitions the string into palindromes.
DSA Rust
fn backtrack(s: &str, start: usize, current: &mut Vec<String>, result: &mut Vec<Vec<String>>) {
if start == s.len() {
result.push(current.clone());
return;
}
for end in start + 1..=[1] {
let substring = &s[start..end];
if [2](substring) {
current.push(substring.to_string());
backtrack(s, end, current, result);
current.pop();
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using s.len() + 1 causes out-of-range error in Rust slicing.
Checking palindrome incorrectly or missing the function call.
✗ Incorrect
The loop ends at s.len() and we check palindrome using is_palindrome function.
5fill in blank
hardFill all three blanks to complete the main function that calls backtracking and returns all palindrome partitions.
DSA Rust
fn partition(s: String) -> Vec<Vec<String>> {
let mut result = Vec::new();
let mut current = Vec::new();
backtrack(&s, [1], &mut current, &mut result);
[2]
}
fn backtrack(s: &str, start: usize, current: &mut Vec<String>, result: &mut Vec<Vec<String>>) {
if start == s.len() {
result.push(current.clone());
return;
}
for end in start + 1..=[3] {
let substring = &s[start..end];
if is_palindrome(substring) {
current.push(substring.to_string());
backtrack(s, end, current, result);
current.pop();
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 1 misses the first character.
Returning current instead of result.
Loop ending before full string length.
✗ Incorrect
Start index is 0, return the result vector, and loop ends at s.len().
