📖 Scenario: You are working on a text editor feature that highlights the longest palindrome inside a given text. A palindrome is a word or phrase that reads the same backward as forward, like "madam" or "racecar".To build this feature, you need to find the longest palindromic substring inside a string entered by the user.
🎯 Goal: Build a Rust program that finds the longest palindromic substring inside a given string.You will first set up the input string, then create helper variables, implement the core logic to find the longest palindrome, and finally print the result.
📋 What You'll Learn
Create a variable input with the exact string "babad"
Create two variables start and max_len both set to 0
Write a function expand_around_center that takes input, left, and right indexes and returns the length of the palindrome
Use a for loop with variable i from 0 to input.len() to find the longest palindrome
Print the longest palindromic substring found from input
💡 Why This Matters
🌍 Real World
Finding palindromic substrings is useful in text editors, DNA sequence analysis, and data validation.
💼 Career
Understanding string manipulation and algorithmic thinking is important for software development and coding interviews.
Progress0 / 4 steps
1
Set up the input string
Create a variable called input and set it to the exact string "babad".
DSA Rust
Hint
Use let input = "babad"; to create the string variable.
2
Create helper variables
Create two variables called start and max_len and set both to 0.
DSA Rust
Hint
Use let mut start = 0; and let mut max_len = 0; to create mutable variables.
3
Implement palindrome expansion function and core logic
Write a function called expand_around_center that takes input as a string slice, left and right as usize, and returns the length of the palindrome expanding around these centers. Then, use a for loop with variable i from 0 to input.len() to find the longest palindromic substring by checking odd and even length palindromes. Update start and max_len accordingly.
DSA Rust
Hint
Use a function to expand around center indexes and a loop to check all centers.
4
Print the longest palindromic substring
Print the longest palindromic substring from input using start and max_len. Use println! with &input[start..start + max_len].
DSA Rust
Hint
Use println!("{}", &input[start..start + max_len]); to print the substring.
Practice
(1/5)
1. What is the main goal of the Longest Palindromic Substring problem?
easy
A. Find the longest substring that contains only vowels
B. Find the longest substring with all unique characters
C. Find the longest substring with increasing characters
D. Find the longest substring that reads the same forwards and backwards
Solution
Step 1: Understand palindrome definition
A palindrome reads the same forwards and backwards, like 'madam' or 'racecar'.
Step 2: Identify problem goal
The problem asks to find the longest continuous part of a string that is a palindrome.
Final Answer:
Find the longest substring that reads the same forwards and backwards -> Option D
Looking for unique characters instead of palindrome
Ignoring continuous substring requirement
2. Which Rust code snippet correctly declares a function header for finding the longest palindromic substring in a string slice s?
easy
A. fn longest_palindrome(s: &str) -> &str { }
B. fn longest_palindrome(s: String) -> String { }
C. fn longest_palindrome(s: &String) -> &String { }
D. fn longest_palindrome(s: str) -> str { }
Solution
Step 1: Understand Rust string types
Rust uses &str for string slices and String for owned strings.
Step 2: Choose correct function signature
Function should take a string slice &str and return a string slice &str for substring references.
Final Answer:
fn longest_palindrome(s: &str) -> &str { } -> Option A
Quick Check:
Use &str for slices in function signature [OK]
Hint: Use &str for input and output substrings [OK]
Common Mistakes:
Using owned String when slice is better
Returning owned String unnecessarily
Incorrect parameter types like str without &
3. What will be the output of the following Rust code snippet that finds the longest palindromic substring?
fn main() {
let s = "babad";
let result = longest_palindrome(s);
println!("{}", result);
}
fn longest_palindrome(s: &str) -> &str {
let chars: Vec<char> = s.chars().collect();
let mut start = 0;
let mut max_len = 1;
for i in 0..chars.len() {
let mut left = i as i32;
let mut right = i as i32;
while left >= 0 && right < chars.len() as i32 && chars[left as usize] == chars[right as usize] {
if (right - left + 1) > max_len {
start = left as usize;
max_len = (right - left + 1) as usize;
}
left -= 1;
right += 1;
}
}
&s[start..start + max_len]
}
medium
A. aba
B. bad
C. bab
D. babad
Solution
Step 1: Understand the code logic
The code expands around each character to find odd-length palindromes and tracks the longest.
Step 2: Trace the input "babad"
At index 0: palindrome "b" (length 1), index 1: "bab" (length 3), index 2: "aba" (length 3). The first longest palindrome found is "bab".
Final Answer:
bab -> Option C
Quick Check:
Longest palindrome substring = bab [OK]
Hint: Expand around center, pick first longest palindrome [OK]
Common Mistakes:
Assuming palindrome must be unique
Returning entire string instead of substring
Confusing substring indices
4. Identify the bug in this Rust function that tries to find the longest palindromic substring:
fn longest_palindrome(s: &str) -> &str {
let chars: Vec<char> = s.chars().collect();
let mut start = 0;
let mut max_len = 1;
for i in 0..chars.len() {
let mut left = i as i32;
let mut right = i as i32 + 1;
while left >= 0 && right < chars.len() as i32 && chars[left as usize] == chars[right as usize] {
if (right - left + 1) > max_len {
start = left as usize;
max_len = (right - left + 1) as usize;
}
left -= 1;
right += 1;
}
}
&s[start..start + max_len]
}
medium
A. The max_len should start at 0, not 1
B. The initial right pointer should be equal to i, not i + 1
C. The loop should iterate from 1 to length, not 0 to length
D. The function should return a String, not &str
Solution
Step 1: Analyze pointer initialization
Right pointer starts at i + 1, which only checks even-length palindromes, missing odd-length ones.
Step 2: Correct pointer for odd-length palindromes
To check odd-length palindromes, right pointer should start at i, same as left pointer.
Final Answer:
The initial right pointer should be equal to i, not i + 1 -> Option B
Quick Check:
Right pointer = i for odd-length palindrome check [OK]
Hint: Start right pointer same as left for odd palindromes [OK]
Common Mistakes:
Only checking even-length palindromes
Wrong loop range causing index errors
Returning wrong type causing borrow issues
5. Given the string "forgeeksskeegfor", which approach correctly finds the longest palindromic substring and what is that substring?
hard
A. Expand around center method; longest palindrome is "geeksskeeg"
B. Dynamic programming with substring table; longest palindrome is "forgeeksskeegfor"
C. Brute force checking all substrings; longest palindrome is "ss"
D. Using hash map for character counts; longest palindrome is "ks"
Solution
Step 1: Understand approaches to find longest palindrome
Expand around center and dynamic programming are common methods; brute force is slow but correct.
Step 2: Identify longest palindrome in given string
"forgeeksskeegfor" contains "geeksskeeg" as the longest palindrome substring, not the entire string.
Step 3: Match approach and answer
Expand around center method efficiently finds "geeksskeeg"; dynamic programming would too but not the full string.
Final Answer:
Expand around center method; longest palindrome is "geeksskeeg" -> Option A
Quick Check:
Longest palindrome = geeksskeeg by center expansion [OK]
Hint: Expand around centers to find longest palindrome fast [OK]
Common Mistakes:
Assuming entire string is palindrome
Using character counts instead of substring checks
Confusing brute force with efficient methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.