diff --git a/Python/faulty_keyboard.py b/Python/faulty_keyboard.py new file mode 100644 index 0000000..ed535fb --- /dev/null +++ b/Python/faulty_keyboard.py @@ -0,0 +1,23 @@ +# Faulty Keyboard + +class Solution(object): + def finalString(self, s): + result = '' + is_reversed = False + + for char in s: + if char == 'i': + is_reversed = not is_reversed + else: + if is_reversed: + result = char + result + else: + result += char + + if is_reversed: + result = result[::-1] + + return result + +solution = Solution() +print(solution.finalString("string")) diff --git a/Python/length_of_longest_subarray_with_at_most_k_frequency.py b/Python/length_of_longest_subarray_with_at_most_k_frequency.py new file mode 100644 index 0000000..47c15f3 --- /dev/null +++ b/Python/length_of_longest_subarray_with_at_most_k_frequency.py @@ -0,0 +1,13 @@ +class Solution(object): + def maxSubarrayLength(self, nums, k): + n, left, result = len(nums), 0, 0 + freq = defaultdict(int) + + for right in range(n): + freq[nums[right]] += 1 + while freq[nums[right]] > k: + freq[nums[left]] -= 1 + left += 1 + result = max(result, right - left + 1) + + return result diff --git a/README.md b/README.md index 4d4d3dc..3af97e5 100644 --- a/README.md +++ b/README.md @@ -228,4 +228,6 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file - [Subarray Product Less Than K](Python/subarray_product_less_than_k.py) - [Valid Parentheses](Python/valid_parentheses.py) - [Remove Element](Python/remove_element.py) -- [Remove Duplicates from Sorted Array](Python/remove_duplicates_from_sorted_array.py) \ No newline at end of file +- [Remove Duplicates from Sorted Array](Python/remove_duplicates_from_sorted_array.py) +- [Faulty Keyboard](Python/faulty_keyboard.py) +- [Length of Longest Subarray With at Most K Frequency](Python/length_of_longest_subarray_with_at_most_k_frequency.py)