From 7a5d0a0646ada57181b8d4ef71dd1f7335a695ef Mon Sep 17 00:00:00 2001 From: ryosuketc <43229670+ryosuketc@users.noreply.github.com> Date: Wed, 29 Oct 2025 09:34:36 +0900 Subject: [PATCH] 409. Longest Palindrome https://leetcode.com/problems/longest-palindrome/ --- 409_longest_palindrome/memo.md | 28 +++++++++++++++++ 409_longest_palindrome/step1.cpp | 53 +++++++++++++++++++++++++++++++ 409_longest_palindrome/step2.cpp | 54 ++++++++++++++++++++++++++++++++ 409_longest_palindrome/step3.cpp | 0 4 files changed, 135 insertions(+) create mode 100644 409_longest_palindrome/memo.md create mode 100644 409_longest_palindrome/step1.cpp create mode 100644 409_longest_palindrome/step2.cpp create mode 100644 409_longest_palindrome/step3.cpp diff --git a/409_longest_palindrome/memo.md b/409_longest_palindrome/memo.md new file mode 100644 index 0000000..cb7289c --- /dev/null +++ b/409_longest_palindrome/memo.md @@ -0,0 +1,28 @@ +# 409. Longest Palindrome + +https://leetcode.com/problems/longest-palindrome/ + +## Comments + +### step1 + +* 最初、与えられた文字列の substring の中で最長の文字列を返すのかと思って 10 分くらい考えながら書いていた。途中で問題設定が違っているのに気付いたのでやり直し + * https://leetcode.com/problems/longest-palindromic-substring/editorial/ + * ちなみに今書きかけのやり方だと、start_index は center の文字の想定なので、奇数長さの palindrome しか取れない。 + * 正しくは偶数長さも考える必要があるので、2 つ index (e.g. `i, j`)を取るようにして、`i, i` と `i, i + 1` から expand していく必要がある +* 気を取り直して問題を見直してから再度取り組み + * 割とすんなり書いた。 + +### step2 + +* このあたりの解放がある + * LeetCode の editorial とほぼ同じ解答になっているようだ + * https://github.com/huyfififi/coding-challenges/pull/17/files + * https://github.com/Kitaken0107/GrindEasy/pull/27/files +* step1 の解法以外だと、概ね map を構築しながら 1 pass O(n) (vs. O(2n) in step1) で解く方法と、set を使って 2 ずつ足しながら書く方法がある。 + * ただ個人的には 2 pass が一番素直で、面接で期待されるとしたらこれかなと思う。ここからどう発展問題にしていくかはあまり思いつかないけど、上記の substr の問題に切り替えるとかかな。あまり今の問題の延長線上という感じはしないけど。 +* else なくそうとすると `Solution1` のようには書けるけど読みにくく感じる。`Solution2` でも、`step1.Solution` でもどっちでもいいかなという気がする。 + +### step3 + +* 省略 diff --git a/409_longest_palindrome/step1.cpp b/409_longest_palindrome/step1.cpp new file mode 100644 index 0000000..84e7635 --- /dev/null +++ b/409_longest_palindrome/step1.cpp @@ -0,0 +1,53 @@ +// Solving wrong question (書きかけで放置). +class Solution { +private: + int longestPalindromeStartingAt(string s, int start_index) { + string longest_palindrome; + int left = start_index; + int right = start_index; + while (left >= 0 && right < s.length) { + if s[left] != s[right] { + break; + } + longest_palindrome = s[left: right + 1] + --left; + ++right; + } + return + } +public: + int longestPalindrome(string s) { + string longest_palindrome; + for (i = 0; i < s.length; ++i) { + } + + } +}; + +// Correct solution +#include + +class Solution { +public: + int longestPalindrome(string s) { + std::unordered_map char_to_frequency; + bool has_odd_frequency = false; + int palindrome_length = 0; + for (char c : s) { + ++char_to_frequency[c]; + } + for (const auto& [c, frequency] : char_to_frequency) { + if (frequency % 2 == 1) { + has_odd_frequency = true; + palindrome_length += frequency - 1; + } else { + palindrome_length += frequency; + } + } + if (has_odd_frequency) { + // Can add one char in the center. + return palindrome_length + 1; + } + return palindrome_length; + } +}; diff --git a/409_longest_palindrome/step2.cpp b/409_longest_palindrome/step2.cpp new file mode 100644 index 0000000..4bdeedb --- /dev/null +++ b/409_longest_palindrome/step2.cpp @@ -0,0 +1,54 @@ +#include + +class Solution1 { +public: + int longestPalindrome(string s) { + std::unordered_map char_to_frequency; + bool has_odd_frequency = false; + int palindrome_length = 0; + for (char c : s) { + ++char_to_frequency[c]; + } + for (const auto& [c, frequency] : char_to_frequency) { + palindrome_length += frequency; + if (frequency % 2 == 1) { + has_odd_frequency = true; + // if odd, we should not add all frequency. + palindrome_length -= 1; + } + } + if (has_odd_frequency) { + // Can add one char in the center. + return palindrome_length + 1; + } + return palindrome_length; + } +}; + + +#include + +class Solution2 { +public: + int longestPalindrome(string s) { + std::unordered_map char_to_frequency; + bool has_odd_frequency = false; + int palindrome_length = 0; + for (char c : s) { + ++char_to_frequency[c]; + } + for (const auto& [c, frequency] : char_to_frequency) { + if (frequency % 2 == 0) { + palindrome_length += frequency; + continue; + } + has_odd_frequency = true; + palindrome_length += frequency - 1; + } + if (has_odd_frequency) { + // Can add one char in the center. + return palindrome_length + 1; + } + return palindrome_length; + } +}; diff --git a/409_longest_palindrome/step3.cpp b/409_longest_palindrome/step3.cpp new file mode 100644 index 0000000..e69de29