From a783807b6ffa0d1008d044771f80a8c9ca553e45 Mon Sep 17 00:00:00 2001 From: Dale Seo <5466341+DaleSeo@users.noreply.github.com> Date: Sun, 27 Sep 2026 12:57:06 -0400 Subject: [PATCH] feat: solve longest palindromic substring --- longest-palindromic-substring/DaleSeo.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 longest-palindromic-substring/DaleSeo.rs diff --git a/longest-palindromic-substring/DaleSeo.rs b/longest-palindromic-substring/DaleSeo.rs new file mode 100644 index 0000000000..192dc1ea98 --- /dev/null +++ b/longest-palindromic-substring/DaleSeo.rs @@ -0,0 +1,23 @@ +// TC: O(n^2) +// SC: O(1) +impl Solution { + pub fn longest_palindrome(s: String) -> String { + let bytes = s.as_bytes(); + let mut best = 0..0; + + for center in 0..bytes.len() { + for end in center..=center + 1 { + let (mut left, mut right) = (center, end); + while left > 0 && right < bytes.len() && bytes[left - 1] == bytes[right] { + left -= 1; + right += 1; + } + if right - left > best.len() { + best = left..right; + } + } + } + + s[best].to_owned() + } +}