-
Notifications
You must be signed in to change notification settings - Fork 0
409. Longest Palindrome #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ryosuketc
wants to merge
1
commit into
main
Choose a base branch
from
409_longest_palindrome
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
|
||
| * 省略 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <unordered_map> | ||
|
|
||
| class Solution { | ||
| public: | ||
| int longestPalindrome(string s) { | ||
| std::unordered_map<char, int> 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; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| #include <unordered_map> | ||
|
|
||
| class Solution1 { | ||
| public: | ||
| int longestPalindrome(string s) { | ||
| std::unordered_map<char, int> 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 <unordered_map> | ||
|
|
||
| class Solution2 { | ||
| public: | ||
| int longestPalindrome(string s) { | ||
| std::unordered_map<char, int> char_to_frequency; | ||
| bool has_odd_frequency = false; | ||
| int palindrome_length = 0; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 変数の宣言遅らせてもいいでしょう。 |
||
| 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; | ||
| } | ||
| }; | ||
Empty file.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
そっちの問題もあったはずです。