From b1e33e8ea88279702c02dbb16067cdcd26f1ecf3 Mon Sep 17 00:00:00 2001 From: ryosuketc <43229670+ryosuketc@users.noreply.github.com> Date: Sun, 11 Jan 2026 05:09:41 +0900 Subject: [PATCH] 3. Longest Substring Without Repeating Characters https://leetcode.com/problems/longest-substring-without-repeating-characters/ --- .../memo.md | 24 +++++++++++++++++++ .../step1.cpp | 19 +++++++++++++++ .../step2.cpp | 20 ++++++++++++++++ .../step3.cpp | 0 4 files changed, 63 insertions(+) create mode 100644 3_longest_substring_without_repeating_characters/memo.md create mode 100644 3_longest_substring_without_repeating_characters/step1.cpp create mode 100644 3_longest_substring_without_repeating_characters/step2.cpp create mode 100644 3_longest_substring_without_repeating_characters/step3.cpp diff --git a/3_longest_substring_without_repeating_characters/memo.md b/3_longest_substring_without_repeating_characters/memo.md new file mode 100644 index 0000000..94ae63a --- /dev/null +++ b/3_longest_substring_without_repeating_characters/memo.md @@ -0,0 +1,24 @@ +# 3. Longest Substring Without Repeating Characters + +https://leetcode.com/problems/longest-substring-without-repeating-characters/ + +## Comments + +### step1 + +* Arai60 で解いた問題なのでさらっと + * https://github.com/ryosuketc/leetcode_arai60/pull/37/files +* 愚直に O(n^2) で探索もできるなあ、などと思いつつ、sliding window として解く +* 最初 `chars_in_substring.contains(s[right])` を if 文にしたうえで `while` の条件に使ってしまっていたりしたが書いている途中でさっと書き直した。 +* 最初 `max_substring_length = s.size()` で初期化してしまっており WA になった。 +* 9:00 くらいで AC。あまり前解いたときのことを覚えていなかったので思ったより時間かかった。 + +### step2 + +* step1 の弧ー0度はまあそれ自体は別にそれでいいかなという感じ。 + * 強いて言えば for でなく while で書いたほうが left, right に対称性があってきれいかもしれない +* index 管理しておいて、left を一気に飛ばすやり方忘れていた。さっと書いてみる + +### step3 + +* skip diff --git a/3_longest_substring_without_repeating_characters/step1.cpp b/3_longest_substring_without_repeating_characters/step1.cpp new file mode 100644 index 0000000..7be2533 --- /dev/null +++ b/3_longest_substring_without_repeating_characters/step1.cpp @@ -0,0 +1,19 @@ +#include + +class Solution { +public: + int lengthOfLongestSubstring(string s) { + int left = 0; + std::unordered_set chars_in_substring; + int max_substring_length = 0; + for (int right = 0; right < s.size(); ++right) { + while (chars_in_substring.contains(s[right])) { + chars_in_substring.erase(s[left]); + ++left; + } + chars_in_substring.insert(s[right]); + max_substring_length = std::max(max_substring_length, right - left + 1); + } + return max_substring_length; + } +}; diff --git a/3_longest_substring_without_repeating_characters/step2.cpp b/3_longest_substring_without_repeating_characters/step2.cpp new file mode 100644 index 0000000..3bc3519 --- /dev/null +++ b/3_longest_substring_without_repeating_characters/step2.cpp @@ -0,0 +1,20 @@ +#include + +class Solution { +public: + int lengthOfLongestSubstring(string s) { + int left = 0; + int right = 0; + int max_substring_length = 0; + std::unordered_map seen_index; + while (right < s.size()) { + if (seen_index.contains(s[right])) { + left = std::max(left, seen_index[s[right]] + 1); + } + max_substring_length = std::max(max_substring_length, right - left + 1); + seen_index[s[right]] = right; + ++right; + } + return max_substring_length; + } +}; diff --git a/3_longest_substring_without_repeating_characters/step3.cpp b/3_longest_substring_without_repeating_characters/step3.cpp new file mode 100644 index 0000000..e69de29