From 31d30f88ace15ec1dd0d41d46f43a6f3e82dea82 Mon Sep 17 00:00:00 2001 From: tom4649 Date: Fri, 29 May 2026 10:50:55 +0900 Subject: [PATCH] 767.Reorganize String --- 0767.Reorganize-String/memo.md | 15 +++++++++++++++ 0767.Reorganize-String/step1.py | 32 ++++++++++++++++++++++++++++++++ 0767.Reorganize-String/step2.py | 30 ++++++++++++++++++++++++++++++ 0767.Reorganize-String/step3.py | 29 +++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 0767.Reorganize-String/memo.md create mode 100644 0767.Reorganize-String/step1.py create mode 100644 0767.Reorganize-String/step2.py create mode 100644 0767.Reorganize-String/step3.py diff --git a/0767.Reorganize-String/memo.md b/0767.Reorganize-String/memo.md new file mode 100644 index 0000000..ca9187a --- /dev/null +++ b/0767.Reorganize-String/memo.md @@ -0,0 +1,15 @@ +# 767.Reorganize String + +## step1 + +heapを使う解法を思いつく + +時間計算量は、文字列をK種類としたときO(Nlog K)。今K=26なのでほぼ線形。 + +## step2 + +https://leetcode.com/problems/reorganize-string/solutions/3947780/100-2-approaches-priority-queue-sort-by-66wlu/?envType=problem-list-v2&envId=7p5x763 + +計算量は O(N)。こちらの方が速い。 + +面接で聞かれたとしたらこちらまで聞かれるのだろうか。 diff --git a/0767.Reorganize-String/step1.py b/0767.Reorganize-String/step1.py new file mode 100644 index 0000000..5b14d87 --- /dev/null +++ b/0767.Reorganize-String/step1.py @@ -0,0 +1,32 @@ +import heapq +import collections + + +class Solution: + def reorganizeString(self, s: str) -> str: + if len(s) == 1: + return s + + counter = collections.Counter(s) + most_common = counter.most_common() + + if most_common[0][1] > (len(s) - 1) // 2 + 1: + return "" + + count_and_letter = [(count, c) for (c, count) in most_common] + heapq.heapify_max(count_and_letter) + result = [] + + while len(result) < len(s): + count, c = heapq.heappop_max(count_and_letter) + result.append(c) + count_next = 0 + if len(count_and_letter) > 0: + count_next, c_next = heapq.heappop_max(count_and_letter) + result.append(c_next) + if count > 1: + heapq.heappush_max(count_and_letter, (count - 1, c)) + if count_next > 1: + heapq.heappush_max(count_and_letter, (count_next - 1, c_next)) + + return "".join(result) diff --git a/0767.Reorganize-String/step2.py b/0767.Reorganize-String/step2.py new file mode 100644 index 0000000..ae4f1df --- /dev/null +++ b/0767.Reorganize-String/step2.py @@ -0,0 +1,30 @@ +import collections + + +class Solution: + def reorganizeString(self, s: str) -> str: + counter = collections.Counter(s) + n = len(s) + + most_common_char, max_count = counter.most_common(1)[0] + + if max_count > (n + 1) // 2: + return "" + + result = [""] * n + index = 0 + + for _ in range(max_count): + result[index] = most_common_char + index += 2 + + del counter[most_common_char] + + for char, count in counter.items(): + for _ in range(count): + if index >= n: + index = 1 + result[index] = char + index += 2 + + return "".join(result) diff --git a/0767.Reorganize-String/step3.py b/0767.Reorganize-String/step3.py new file mode 100644 index 0000000..13f8987 --- /dev/null +++ b/0767.Reorganize-String/step3.py @@ -0,0 +1,29 @@ +import collections + + +class Solution: + def reorganizeString(self, s: str) -> str: + counter = collections.Counter(s) + most_common_letter, most_common_count = counter.most_common()[0] + + if most_common_count > (len(s) + 1) // 2: + return "" + + result = [""] * len(s) + index = 0 + while most_common_count > 0: + result[index] = most_common_letter + index += 2 + most_common_count -= 1 + + del counter[most_common_letter] + + for letter, count in counter.items(): + while count > 0: + if index >= len(s): + index = 1 + result[index] = letter + index += 2 + count -= 1 + + return "".join(result)