From 9054ae25c450a5700d9b14963e8914aa7ef5e691 Mon Sep 17 00:00:00 2001 From: Sayko <161838323+SaykoMe@users.noreply.github.com> Date: Fri, 29 Mar 2024 05:58:37 +0500 Subject: [PATCH 1/4] Resolved 'Faulty Keyboard' LeetCode problem with an optimized solution --- Python/faulty_keyboard.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Python/faulty_keyboard.py 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")) From e0b2b44360b57099520addc5add0e2f5b5d85cb4 Mon Sep 17 00:00:00 2001 From: Sayko <161838323+SaykoMe@users.noreply.github.com> Date: Fri, 29 Mar 2024 06:01:26 +0500 Subject: [PATCH 2/4] Resolved 'Length of Longest Subarray With at Most K Frequency' LeetCode problem with an optimized solution --- ..._of_longest_subarray_with_at_most_k_frequency.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Python/length_of_longest_subarray_with_at_most_k_frequency.py 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 From 6041c84bb7bc83e002757c6972a702f7b5a5f205 Mon Sep 17 00:00:00 2001 From: Sayko <161838323+SaykoMe@users.noreply.github.com> Date: Fri, 29 Mar 2024 06:02:23 +0500 Subject: [PATCH 3/4] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4d4d3dc..eb11057 100644 --- a/README.md +++ b/README.md @@ -228,4 +228,5 @@ 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) From 24999e457acde0177f03463a3174cab83e3ef3e3 Mon Sep 17 00:00:00 2001 From: Sayko <161838323+SaykoMe@users.noreply.github.com> Date: Fri, 29 Mar 2024 06:02:50 +0500 Subject: [PATCH 4/4] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index eb11057..3af97e5 100644 --- a/README.md +++ b/README.md @@ -230,3 +230,4 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file - [Remove Element](Python/remove_element.py) - [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)