Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions 409_longest_palindrome/memo.md
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 分くらい考えながら書いていた。途中で問題設定が違っているのに気付いたのでやり直し
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そっちの問題もあったはずです。

* 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

* 省略
53 changes: 53 additions & 0 deletions 409_longest_palindrome/step1.cpp
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;
}
};
54 changes: 54 additions & 0 deletions 409_longest_palindrome/step2.cpp
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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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.