Skip to content

Commit f300568

Browse files
committed
feat: add method to validate palindrome
1 parent f8e41b7 commit f300568

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

palindrome_pairs/src/lib.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,49 @@
11
pub struct Solution {}
22

33
impl Solution {
4-
pub fn palindrome_pairs(words: Vec<String>) -> Vec<Vec<i32>> {}
4+
fn is_palindrome(word: &str) -> bool {
5+
let chars = word.chars().collect::<Vec<char>>();
6+
for i in 0..chars.len() {
7+
let j = chars.len() - 1 - i;
8+
if i >= j {
9+
break;
10+
}
11+
if chars[i] != chars[j] {
12+
return false;
13+
}
14+
}
15+
return true;
16+
}
17+
18+
pub fn palindrome_pairs(words: Vec<String>) -> Vec<Vec<i32>> {
19+
vec![]
20+
}
521
}
622

723
#[cfg(test)]
824
mod tests {
925
use super::*;
1026

1127
fn as_expected(words: Vec<&str>, expected: Vec<[i32; 2]>) {
12-
let result = Solution::palindrome_pairs(words.iter().map(|x| x.to_string()).collect());
28+
let mut result = Solution::palindrome_pairs(words.iter().map(|x| x.to_string()).collect());
1329
result.sort();
14-
let expected_vector = expected
30+
let mut expected_vector = expected
1531
.iter()
1632
.map(|x| x.to_vec())
1733
.collect::<Vec<Vec<i32>>>();
1834
expected_vector.sort();
1935
assert_eq!(result, expected_vector);
2036
}
2137

38+
#[test]
39+
fn test_is_palindrome() {
40+
assert!(Solution::is_palindrome("abcddcba"));
41+
assert!(Solution::is_palindrome("dcbaabcd"));
42+
assert!(Solution::is_palindrome("slls"));
43+
assert!(Solution::is_palindrome("llssssll"));
44+
assert!(!Solution::is_palindrome("ssslllls"));
45+
}
46+
2247
#[test]
2348
fn example_1() {
2449
let words = ["abcd", "dcba", "lls", "s", "sssll"];

0 commit comments

Comments
 (0)