Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
32 changes: 32 additions & 0 deletions longest-repeating-character-replacement/dusunax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'''
# 424. Longest Repeating Character Replacement

use sliding window to find the longest substring with at most k replacements.

## Time and Space Complexity

```
TC: O(n)
SC: O(1)
```
'''

class Solution:
def characterReplacement(self, s: str, k: int) -> int:
freq = [0] * 26 # A~Z, SC: O(1)
left = 0
max_freq = 0
result = 0

for right in range(len(s)): # TC: O(n)
curr_char_index = ord(s[right]) - 65
freq[curr_char_index] += 1
max_freq = max(max_freq, freq[curr_char_index])

if (right - left + 1) - max_freq > k:
freq[ord(s[left]) - 65] -= 1
left += 1

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

return result
4 changes: 4 additions & 0 deletions number-of-1-bits/dusunax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Solution:
def hammingWeight(self, n: int) -> int:
binary_string = bin(n).replace('0b', '');
return binary_string.count('1');