Skip to content

Commit

Permalink
add solution of is_palindrome
Browse files Browse the repository at this point in the history
  • Loading branch information
tednaaa committed Sep 3, 2024
1 parent 37eb871 commit b531efc
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
[workspace.lints.clippy]
pedantic = "warn"
nursery = "warn"
nursery = { level = "warn", priority = -1 }
pedantic = { level = "warn", priority = -1 }
needless_pass_by_value = "allow"

[workspace]
members = [
"crates/contains_duplicate",
"crates/is_palindrome",
"crates/two_sum",
"crates/valid_anagram",
]
Expand Down
9 changes: 9 additions & 0 deletions crates/is_palindrome/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "is_palindrome"
version = "0.1.0"
edition = "2021"

[dependencies]

[lints]
workspace = true
57 changes: 57 additions & 0 deletions crates/is_palindrome/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#[must_use]
pub fn is_palindrome(s: String) -> bool {
if s.trim().is_empty() {
return true;
}

let bytes = s.as_bytes();

let mut left_pointer = 0;
let mut right_pointer = bytes.len() - 1;

while left_pointer < right_pointer {
if !bytes[left_pointer].is_ascii_alphanumeric() {
left_pointer += 1;
continue;
}

if !bytes[right_pointer].is_ascii_alphanumeric() {
right_pointer -= 1;
continue;
}

if bytes[left_pointer].to_ascii_lowercase() != bytes[right_pointer].to_ascii_lowercase() {
return false;
}

left_pointer += 1;
right_pointer -= 1;
}

true
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn case_1() {
assert!(is_palindrome(String::from("A man, a plan, a canal: Panama")));
}

#[test]
fn case_2() {
assert!(!is_palindrome(String::from("race a car")));
}

#[test]
fn case_3() {
assert!(is_palindrome(String::from(" ")));
}

#[test]
fn case_4() {
assert!(is_palindrome(String::from("f f")));
}
}
1 change: 0 additions & 1 deletion crates/valid_anagram/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::HashMap;

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

0 comments on commit b531efc

Please sign in to comment.