From 036206b841ac54d6260bddbf2e1d14a5e577ae20 Mon Sep 17 00:00:00 2001 From: OmMahajan29 <112854335+OmMahajan29@users.noreply.github.com> Date: Wed, 25 Sep 2024 07:04:45 +0530 Subject: [PATCH 01/13] Create sliding_window.py created sliding_window algorithm under greedy algorithm section please merge with master --- sliding_window.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 sliding_window.py diff --git a/sliding_window.py b/sliding_window.py new file mode 100644 index 000000000000..b08e6bdfec57 --- /dev/null +++ b/sliding_window.py @@ -0,0 +1,46 @@ +def sliding_window(s: str) -> int: + """ + This function takes a string and returns the length of the longest substring + without repeating characters using the sliding window algorithm. + + Args: + s: A string input. + + Returns: + max_len: Length of the longest substring without repeating characters. + + Examples: + >>> sliding_window("abcabcbb") + 3 + >>> sliding_window("bbbbb") + 1 + >>> sliding_window("pwwkew") + 3 + >>> sliding_window("") + 0 + >>> sliding_window("abcdefg") + 7 + >>> sliding_window("abccba") + 3 + """ + char_index_map = {} + left = 0 + max_len = 0 + + # Traverse the string with a right pointer + for right, char in enumerate(s): + if char in char_index_map and char_index_map[char] >= left: + # Move the left pointer to avoid repeating characters + left = char_index_map[char] + 1 + + # Update the latest index of the character + char_index_map[char] = right + + # Calculate the current length of the window + max_len = max(max_len, right - left + 1) + + return max_len + +if __name__ == "__main__": + import doctest + doctest.testmod() From 78306cc55ad1d895c9d736547353e3c407950788 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 25 Sep 2024 01:36:35 +0000 Subject: [PATCH 02/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sliding_window.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/sliding_window.py b/sliding_window.py index b08e6bdfec57..5ec369e7da94 100644 --- a/sliding_window.py +++ b/sliding_window.py @@ -32,15 +32,17 @@ def sliding_window(s: str) -> int: if char in char_index_map and char_index_map[char] >= left: # Move the left pointer to avoid repeating characters left = char_index_map[char] + 1 - + # Update the latest index of the character char_index_map[char] = right - + # Calculate the current length of the window max_len = max(max_len, right - left + 1) - + return max_len + if __name__ == "__main__": import doctest + doctest.testmod() From 33a8b6511a648ce2adce77e4a89aad3636efa318 Mon Sep 17 00:00:00 2001 From: OmMahajan29 <112854335+OmMahajan29@users.noreply.github.com> Date: Wed, 25 Sep 2024 07:25:59 +0530 Subject: [PATCH 03/13] Rename sliding_window.py to greedy_methods/sliding_window.py properly set the path to be committed to --- sliding_window.py => greedy_methods/sliding_window.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename sliding_window.py => greedy_methods/sliding_window.py (100%) diff --git a/sliding_window.py b/greedy_methods/sliding_window.py similarity index 100% rename from sliding_window.py rename to greedy_methods/sliding_window.py From 6a8bfb08783034d756bc211058903735f3dc5e41 Mon Sep 17 00:00:00 2001 From: OmMahajan29 <112854335+OmMahajan29@users.noreply.github.com> Date: Wed, 25 Sep 2024 07:31:14 +0530 Subject: [PATCH 04/13] Update sliding_window.py changed input parameter --- greedy_methods/sliding_window.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/greedy_methods/sliding_window.py b/greedy_methods/sliding_window.py index 5ec369e7da94..975f88f2997f 100644 --- a/greedy_methods/sliding_window.py +++ b/greedy_methods/sliding_window.py @@ -1,10 +1,10 @@ -def sliding_window(s: str) -> int: +def sliding_window(input_string: str) -> int: """ This function takes a string and returns the length of the longest substring without repeating characters using the sliding window algorithm. Args: - s: A string input. + input_string: A string input. Returns: max_len: Length of the longest substring without repeating characters. @@ -28,21 +28,19 @@ def sliding_window(s: str) -> int: max_len = 0 # Traverse the string with a right pointer - for right, char in enumerate(s): + for right, char in enumerate(input_string): if char in char_index_map and char_index_map[char] >= left: # Move the left pointer to avoid repeating characters left = char_index_map[char] + 1 - + # Update the latest index of the character char_index_map[char] = right - + # Calculate the current length of the window max_len = max(max_len, right - left + 1) - + return max_len - if __name__ == "__main__": import doctest - doctest.testmod() From 6a40d5ac3449388f865fcc36740d37e24331b4f8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 25 Sep 2024 02:01:55 +0000 Subject: [PATCH 05/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/sliding_window.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/greedy_methods/sliding_window.py b/greedy_methods/sliding_window.py index 975f88f2997f..1f877111e5b2 100644 --- a/greedy_methods/sliding_window.py +++ b/greedy_methods/sliding_window.py @@ -32,15 +32,17 @@ def sliding_window(input_string: str) -> int: if char in char_index_map and char_index_map[char] >= left: # Move the left pointer to avoid repeating characters left = char_index_map[char] + 1 - + # Update the latest index of the character char_index_map[char] = right - + # Calculate the current length of the window max_len = max(max_len, right - left + 1) - + return max_len + if __name__ == "__main__": import doctest + doctest.testmod() From 9f7907437985157c661b64cd178d132e56085ca7 Mon Sep 17 00:00:00 2001 From: OmMahajan29 <112854335+OmMahajan29@users.noreply.github.com> Date: Wed, 25 Sep 2024 08:22:06 +0530 Subject: [PATCH 06/13] Update sliding_window.py Removed whitespaces --- greedy_methods/sliding_window.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/greedy_methods/sliding_window.py b/greedy_methods/sliding_window.py index 1f877111e5b2..a7b062b44630 100644 --- a/greedy_methods/sliding_window.py +++ b/greedy_methods/sliding_window.py @@ -32,16 +32,12 @@ def sliding_window(input_string: str) -> int: if char in char_index_map and char_index_map[char] >= left: # Move the left pointer to avoid repeating characters left = char_index_map[char] + 1 - # Update the latest index of the character char_index_map[char] = right - # Calculate the current length of the window max_len = max(max_len, right - left + 1) - return max_len - if __name__ == "__main__": import doctest From a702d5425caf25eeb7d913094b4aeaa138c37492 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 25 Sep 2024 02:52:27 +0000 Subject: [PATCH 07/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/sliding_window.py | 1 + 1 file changed, 1 insertion(+) diff --git a/greedy_methods/sliding_window.py b/greedy_methods/sliding_window.py index a7b062b44630..4854dcc6d540 100644 --- a/greedy_methods/sliding_window.py +++ b/greedy_methods/sliding_window.py @@ -38,6 +38,7 @@ def sliding_window(input_string: str) -> int: max_len = max(max_len, right - left + 1) return max_len + if __name__ == "__main__": import doctest From 3800ca6e24bfe51a0cd89d75b86d5084ed3d899a Mon Sep 17 00:00:00 2001 From: OmMahajan29 <112854335+OmMahajan29@users.noreply.github.com> Date: Wed, 25 Sep 2024 08:31:36 +0530 Subject: [PATCH 08/13] Update sliding_window.py Check for type annotationss --- greedy_methods/sliding_window.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/greedy_methods/sliding_window.py b/greedy_methods/sliding_window.py index 4854dcc6d540..2d1de02dc5df 100644 --- a/greedy_methods/sliding_window.py +++ b/greedy_methods/sliding_window.py @@ -23,7 +23,8 @@ def sliding_window(input_string: str) -> int: >>> sliding_window("abccba") 3 """ - char_index_map = {} + # Type annotation for char_index_map: keys are single characters (str), values are indices (int) + char_index_map: dict[str, int] = {} left = 0 max_len = 0 @@ -31,15 +32,14 @@ def sliding_window(input_string: str) -> int: for right, char in enumerate(input_string): if char in char_index_map and char_index_map[char] >= left: # Move the left pointer to avoid repeating characters - left = char_index_map[char] + 1 + left = char_index_map[char] + 1 # Update the latest index of the character - char_index_map[char] = right + char_index_map[char] = right # Calculate the current length of the window max_len = max(max_len, right - left + 1) return max_len - if __name__ == "__main__": import doctest - doctest.testmod() + From efa26a8bac2c65948de6d2de10d9ff7665ace90d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 25 Sep 2024 03:01:57 +0000 Subject: [PATCH 09/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/sliding_window.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/greedy_methods/sliding_window.py b/greedy_methods/sliding_window.py index 2d1de02dc5df..71d8745f5992 100644 --- a/greedy_methods/sliding_window.py +++ b/greedy_methods/sliding_window.py @@ -32,14 +32,15 @@ def sliding_window(input_string: str) -> int: for right, char in enumerate(input_string): if char in char_index_map and char_index_map[char] >= left: # Move the left pointer to avoid repeating characters - left = char_index_map[char] + 1 + left = char_index_map[char] + 1 # Update the latest index of the character - char_index_map[char] = right + char_index_map[char] = right # Calculate the current length of the window max_len = max(max_len, right - left + 1) return max_len + if __name__ == "__main__": import doctest + doctest.testmod() - From deb5cf11e093788868956973b031d02d5f11d70e Mon Sep 17 00:00:00 2001 From: OmMahajan29 <112854335+OmMahajan29@users.noreply.github.com> Date: Wed, 25 Sep 2024 08:34:54 +0530 Subject: [PATCH 10/13] Update sliding_window.py --- greedy_methods/sliding_window.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/greedy_methods/sliding_window.py b/greedy_methods/sliding_window.py index 71d8745f5992..274d25c9ae18 100644 --- a/greedy_methods/sliding_window.py +++ b/greedy_methods/sliding_window.py @@ -23,7 +23,6 @@ def sliding_window(input_string: str) -> int: >>> sliding_window("abccba") 3 """ - # Type annotation for char_index_map: keys are single characters (str), values are indices (int) char_index_map: dict[str, int] = {} left = 0 max_len = 0 @@ -39,7 +38,6 @@ def sliding_window(input_string: str) -> int: max_len = max(max_len, right - left + 1) return max_len - if __name__ == "__main__": import doctest From 65abb5f52d2e5325c27c40a3226bbbeb7717ff4b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 25 Sep 2024 03:05:16 +0000 Subject: [PATCH 11/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/sliding_window.py | 1 + 1 file changed, 1 insertion(+) diff --git a/greedy_methods/sliding_window.py b/greedy_methods/sliding_window.py index 274d25c9ae18..62adde51acac 100644 --- a/greedy_methods/sliding_window.py +++ b/greedy_methods/sliding_window.py @@ -38,6 +38,7 @@ def sliding_window(input_string: str) -> int: max_len = max(max_len, right - left + 1) return max_len + if __name__ == "__main__": import doctest From 2a614cfd5c4ec55c01339c9787f674b8a4fd04ff Mon Sep 17 00:00:00 2001 From: OmMahajan29 <112854335+OmMahajan29@users.noreply.github.com> Date: Sun, 29 Sep 2024 11:59:41 +0530 Subject: [PATCH 12/13] Update sliding_window.py enhanced further --- greedy_methods/sliding_window.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/greedy_methods/sliding_window.py b/greedy_methods/sliding_window.py index 62adde51acac..8eeffa88f79f 100644 --- a/greedy_methods/sliding_window.py +++ b/greedy_methods/sliding_window.py @@ -2,13 +2,19 @@ def sliding_window(input_string: str) -> int: """ This function takes a string and returns the length of the longest substring without repeating characters using the sliding window algorithm. + + It runs in O(n) time, where n is the length of the string. The sliding window + approach ensures that each character is processed at most twice. Args: input_string: A string input. Returns: - max_len: Length of the longest substring without repeating characters. + int: Length of the longest substring without repeating characters. + Raises: + TypeError: If the input is not a string. + Examples: >>> sliding_window("abcabcbb") 3 @@ -22,11 +28,23 @@ def sliding_window(input_string: str) -> int: 7 >>> sliding_window("abccba") 3 + >>> sliding_window("a"*10000) + 1 """ + # Error handling for non-string inputs + if not isinstance(input_string, str): + raise TypeError("Input must be a string") + + # Handle empty string case immediately + if len(input_string) == 0: + return 0 + + # Dictionary to store the most recent index of each character char_index_map: dict[str, int] = {} - left = 0 - max_len = 0 + # Initialize the sliding window pointers + left: int = 0 + max_len: int = 0 # Traverse the string with a right pointer for right, char in enumerate(input_string): if char in char_index_map and char_index_map[char] >= left: @@ -35,11 +53,10 @@ def sliding_window(input_string: str) -> int: # Update the latest index of the character char_index_map[char] = right # Calculate the current length of the window - max_len = max(max_len, right - left + 1) + current_len = right - left + 1 + max_len = max(max_len, current_len) return max_len - if __name__ == "__main__": import doctest - doctest.testmod() From 36d4342bf5d9d911e6cd9d37e558aa1b90f9ef29 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 29 Sep 2024 06:30:01 +0000 Subject: [PATCH 13/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/sliding_window.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/greedy_methods/sliding_window.py b/greedy_methods/sliding_window.py index 8eeffa88f79f..b3124fc74570 100644 --- a/greedy_methods/sliding_window.py +++ b/greedy_methods/sliding_window.py @@ -2,7 +2,7 @@ def sliding_window(input_string: str) -> int: """ This function takes a string and returns the length of the longest substring without repeating characters using the sliding window algorithm. - + It runs in O(n) time, where n is the length of the string. The sliding window approach ensures that each character is processed at most twice. @@ -14,7 +14,7 @@ def sliding_window(input_string: str) -> int: Raises: TypeError: If the input is not a string. - + Examples: >>> sliding_window("abcabcbb") 3 @@ -57,6 +57,8 @@ def sliding_window(input_string: str) -> int: max_len = max(max_len, current_len) return max_len + if __name__ == "__main__": import doctest + doctest.testmod()