Created
February 28, 2022 01:46
-
-
Save ericness/cfed12f8daded67f393bbd3b3d324d72 to your computer and use it in GitHub Desktop.
LeetCode 424 Part 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
class Solution: | |
def characterReplacement(self, s: str, k: int) -> int: | |
"""Calculate the longer repeating character | |
string when replacing with one letter | |
Args: | |
s (str): String to evaluate | |
k (int): Number of characters to replace | |
Returns: | |
int: Length of longest repeating sequence | |
""" | |
positions = defaultdict(list) | |
max_length = 0 | |
for index, char in enumerate(s): | |
positions[char].append(index) | |
for indexes in positions.values(): | |
index_length = len(indexes) | |
diffs = [ | |
indexes[j] - indexes[i] - 1 | |
for i, j in zip(range(index_length - 1), range(1, index_length)) | |
] | |
diffs.insert(0, indexes[0]) | |
diffs.append(len(s) - 1 - indexes[-1]) | |
return 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment