Skip to content

Commit b531efc

Browse files
committed
add solution of is_palindrome
1 parent 37eb871 commit b531efc

File tree

5 files changed

+74
-3
lines changed

5 files changed

+74
-3
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
[workspace.lints.clippy]
2-
pedantic = "warn"
3-
nursery = "warn"
2+
nursery = { level = "warn", priority = -1 }
3+
pedantic = { level = "warn", priority = -1 }
4+
needless_pass_by_value = "allow"
45

56
[workspace]
67
members = [
78
"crates/contains_duplicate",
9+
"crates/is_palindrome",
810
"crates/two_sum",
911
"crates/valid_anagram",
1012
]

crates/is_palindrome/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "is_palindrome"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
8+
[lints]
9+
workspace = true

crates/is_palindrome/src/lib.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#[must_use]
2+
pub fn is_palindrome(s: String) -> bool {
3+
if s.trim().is_empty() {
4+
return true;
5+
}
6+
7+
let bytes = s.as_bytes();
8+
9+
let mut left_pointer = 0;
10+
let mut right_pointer = bytes.len() - 1;
11+
12+
while left_pointer < right_pointer {
13+
if !bytes[left_pointer].is_ascii_alphanumeric() {
14+
left_pointer += 1;
15+
continue;
16+
}
17+
18+
if !bytes[right_pointer].is_ascii_alphanumeric() {
19+
right_pointer -= 1;
20+
continue;
21+
}
22+
23+
if bytes[left_pointer].to_ascii_lowercase() != bytes[right_pointer].to_ascii_lowercase() {
24+
return false;
25+
}
26+
27+
left_pointer += 1;
28+
right_pointer -= 1;
29+
}
30+
31+
true
32+
}
33+
34+
#[cfg(test)]
35+
mod tests {
36+
use super::*;
37+
38+
#[test]
39+
fn case_1() {
40+
assert!(is_palindrome(String::from("A man, a plan, a canal: Panama")));
41+
}
42+
43+
#[test]
44+
fn case_2() {
45+
assert!(!is_palindrome(String::from("race a car")));
46+
}
47+
48+
#[test]
49+
fn case_3() {
50+
assert!(is_palindrome(String::from(" ")));
51+
}
52+
53+
#[test]
54+
fn case_4() {
55+
assert!(is_palindrome(String::from("f f")));
56+
}
57+
}

crates/valid_anagram/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::collections::HashMap;
22

33
#[must_use]
4-
#[allow(clippy::needless_pass_by_value)]
54
pub fn is_anagram(s: String, t: String) -> bool {
65
if s.len() != t.len() {
76
return false;

0 commit comments

Comments
 (0)