Skip to content

Commit

Permalink
Improved is_palindrome speed by 30%
Browse files Browse the repository at this point in the history
  • Loading branch information
craigmayhew committed Mar 20, 2024
1 parent b6e69ab commit 0a2deba
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/pages/cruncher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,12 +483,18 @@ mod numerics_to_text {
}
}

/// # Return a bool depending on the string being palindromic
pub fn is_palindrome(string: &str) -> bool {
let half_len = string.len() / 2;
string
.chars()
.take(half_len)
.eq(string.chars().rev().take(half_len))
let mut i = 0;
let mut j = string.len()-1;
while i < j {
if &string.as_bytes()[i] != &string.as_bytes()[j] {
return false;
}
j -= 1;
i += 1;
}
true
}

pub fn nth_root(str_num: &str, n: usize) -> String {
Expand Down

0 comments on commit 0a2deba

Please sign in to comment.