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
27 changes: 27 additions & 0 deletions longest-repeating-character-replacement/DaleSeo.rs
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Sliding Window
  • 설명: 이 코드는 고정 크기의 윈도우를 이동하며 문자 빈도수를 계산하는 슬라이딩 윈도우 패턴을 사용하여 최장 반복 문자 수를 찾는다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// TC: O(n), where n is s.len()
// SC: O(1), fixed 26-slot frequency array
impl Solution {
pub fn character_replacement(s: String, k: i32) -> i32 {
let bytes = s.as_bytes();
let mut counts = [0i32; 26];
let mut start = 0usize;
let mut max_freq = 0i32;
let mut result = 0i32;

for end in 0..bytes.len() {
let idx = (bytes[end] - b'A') as usize;
counts[idx] += 1;
max_freq = max_freq.max(counts[idx]);

while (end - start + 1) as i32 - max_freq > k {
let left_idx = (bytes[start] - b'A') as usize;
counts[left_idx] -= 1;
start += 1;
}

result = result.max((end - start + 1) as i32);
}

result
}
}
16 changes: 16 additions & 0 deletions longest-repeating-character-replacement/sangbeenmoon.py
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Sliding Window, Hash Map / Hash Set
  • 설명: 이 코드는 슬라이딩 윈도우 기법을 사용하여 연속된 문자열 구간을 탐색하며, 해시 맵으로 문자 빈도수를 관리합니다. 윈도우 크기 조절과 빈도수 계산을 통해 최적의 구간을 찾는 방식입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
freq = {}
left = 0
ans = 0

for right in range(len(s)):
freq[s[right]] = freq.get(s[right], 0) + 1

while (right - left + 1) - max(freq.values()) > k:
freq[s[left]] -= 1
left += 1

ans = max(ans, right - left + 1)

return ans
Loading