From 1b1766173dbe1f60f552c647ec3e790a2d0685f1 Mon Sep 17 00:00:00 2001 From: Harshit Gupta Date: Sun, 1 Aug 2021 15:15:14 +0530 Subject: [PATCH 1/4] Add solution for 3rd LeetCode Practice Problem - Longest Substring Without Repeating Characters This commit adds a well-commented and human-readable Python 3 program to solve the third LeetCode Practice Problem viz., "Longest Substring Without Repeating Characters". The code is judged as Accepted on LeetCode, on 1st August 2021 (the same day this PR is initiated). --- ... Substring Without Repeating Characters.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Python/3. Longest Substring Without Repeating Characters.py diff --git a/Python/3. Longest Substring Without Repeating Characters.py b/Python/3. Longest Substring Without Repeating Characters.py new file mode 100644 index 00000000..2815f6f6 --- /dev/null +++ b/Python/3. Longest Substring Without Repeating Characters.py @@ -0,0 +1,29 @@ +""" +LeetCode submission result: + (987 / 987 test cases passed. \ Status: Accepted \ Runtime: 76 ms \ Memory Usage: 14.4 MB) + - available at: https://leetcode.com/submissions/detail/531509506/ +""" + +class Solution: + def lengthOfLongestSubstring(self, string: str) -> int: + + # Creating a charactersCountDict to store count of characters in the current + charactersCountDict = {} + + # declaring variables to mark the Starting Index as well as Maximum Length of any contiguous substring without recurring characters achieved + currentStartingIndex = maxLength = 0 + + # Iterating through all indices of the string, one by one, while analyzing string between 'currentStartingIndex' and this ending 'index'. + for index in range(len(string)): + # In case string character at this index already exists between string[currentStartingIndex:index], then removing the starting of string from currentStartingIndex considered to remove any repeated characters in the considered string + while string[index] in charactersCountDict: + charactersCountDict[string[currentStartingIndex]] -= 1 # Reducing the string character count of string[currentStartingIndex] character so as to eliminate it from current string (in the considered sliding window) + if charactersCountDict[string[currentStartingIndex]] < 1: charactersCountDict.pop(string[currentStartingIndex]) # If current count of this character goes below 1, that means this character no longer exists in the substring, therefore the character key is removed from charactersCountDict counter dictionary + currentStartingIndex += 1 # Shifting the currentStartingIndex one step ahead + + # Now that the while loop has completed, it is assured that this character is not included in the substring string[currentStartingIndex:index], therefore we can safely insert it in string[currentStartingIndex:index+1] (last index excluded in the string slice) + charactersCountDict[string[index]] = 1 + + maxLength = max(maxLength, index-currentStartingIndex+1) # Assessing maxLength to be maximum of current substring with unique character and maximum length achieved at any point of time while carefully sliding the limits + + return maxLength # Finally, returning the desired maximum length of contiguous substring that has no repeating characters \ No newline at end of file From 8b1987af30732e358355fac7132dc532c78f011e Mon Sep 17 00:00:00 2001 From: Harshit Gupta Date: Sun, 1 Aug 2021 15:45:53 +0530 Subject: [PATCH 2/4] Update README.md Sorted Strings Table Entries in ascending order as per the # Problem ID column, and added entry for Python solution to newly added solution to Strings #3: Longest Substring Without Repeating Characters --- README.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b13f42ee..602e32f1 100644 --- a/README.md +++ b/README.md @@ -156,18 +156,19 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu # String | # | Title | Solution | Time | Space | Difficulty | Tag | Note | -| ---- | ----------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ------ | ------ | ---------- | --- | --------------- | +| :--: | ----------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ------ | ------ | ---------- | --- | --------------- | +| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Python](./Python/3.%20Longest%20Substring%20Without%20Repeating%20Characters.py) | _O(n)_ | _O(n)_ | Medium | `Hash Table`
`Sliding Window` | Open for improvisation, mentioned time and space complexities unconfirmed | +| 8 | [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) | [Java](./Java/string-to-integer-atoi.java) | _O(n)_ | _O(1)_ | Medium | | | +| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [Java](./Java/palindrome-number.java) | _O(n)_ | _O(1)_ | Easy | | | +| 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [Java](./Java/reverse-words-in-a-string.java) | _O(1)_ | _O(n)_ | Medium | | | | 383 | [Ransom Note](https://leetcode.com/problems/ransom-note/) | [Java](./Java/ransom-note.java) | _O(1)_ | _O(n)_ | Easy | | Character Count | | 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [Java](./Java/first-unique-character-in-a-string.java) | _O(n)_ | _O(1)_ | Easy | | Character Count | -| 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [Java](./Java/reverse-words-in-a-string.java) | _O(1)_ | _O(n)_ | Medium | | | | 520 | [Detect Capital Use](https://leetcode.com/problems/detect-capital/) | [Java](./Java/detect-capital-use.java) | _O(n)_ | _O(1)_ | Easy | | | +| 767 | [Reorganize String](https://leetcode.com/problems/reorganize-string/) | [Python](./Python/reorganize-string.py) | _O(n)_ | _O(n)_ | Medium | | | +| 859 | [Buddy Strings](https://leetcode.com/problems/buddy-strings/) | [Java](./Java/buddy-strings.java) | _O(n)_ | _O(1)_ | Easy | | | | 1221 | [Split a String in Balanced Strings](https://leetcode.com/problems/split-a-string-in-balanced-strings/) | [Python](./Python/split-a-string-in-balanced-strings.py) | _O(n)_ | _O(1)_ | Easy | | | -| 1614 | [Maximum Nesting Depth of the Parentheses](https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/) | [Java](./Java/max-nesting-depth-parentheses.java) | _O(n)_ | _O(1)_ | Easy | | | | 1374 | [Generate a String With Characters That Have Odd Counts](https://leetcode.com/problems/generate-a-string-with-characters-that-have-odd-counts/) | [Java](./Java/generate-a-string-with-characters-that-have-odd-counts.java) | _O(n)_ | _O(1)_ | Easy | | | -| 859 | [Buddy Strings](https://leetcode.com/problems/buddy-strings/) | [Java](./Java/buddy-strings.java) | _O(n)_ | _O(1)_ | Easy | | | -| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [Java](./Java/palindrome-number.java) | _O(n)_ | _O(1)_ | Easy | | | -| 767 | [Reorganize String](https://leetcode.com/problems/reorganize-string/) | [Python](./Python/reorganize-string.py) | _O(n)_ | _O(n)_ | Medium | | | -| 8 | [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) | [Java](./Java/string-to-integer-atoi.java) | _O(n)_ | _O(1)_ | Medium | | | +| 1614 | [Maximum Nesting Depth of the Parentheses](https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/) | [Java](./Java/max-nesting-depth-parentheses.java) | _O(n)_ | _O(1)_ | Easy | | |
⬆️ Back to Top From 922745dd5fec0a07a8971c5e5ea918a9f9135767 Mon Sep 17 00:00:00 2001 From: Harshit Gupta Date: Sun, 1 Aug 2021 18:01:50 +0530 Subject: [PATCH 3/4] Rename 3. Longest Substring Without Repeating Characters.py to Longest_Substring_Without_Repeating_Characters.py Renamed this file to Longest_Substring_Without_Repeating_Characters.py in order to match file naming conventions of the repository, as mentioned in PULL_REQUEST_TEMPLATE --- ...ers.py => Longest_Substring_Without_Repeating_Characters.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename Python/{3. Longest Substring Without Repeating Characters.py => Longest_Substring_Without_Repeating_Characters.py} (98%) diff --git a/Python/3. Longest Substring Without Repeating Characters.py b/Python/Longest_Substring_Without_Repeating_Characters.py similarity index 98% rename from Python/3. Longest Substring Without Repeating Characters.py rename to Python/Longest_Substring_Without_Repeating_Characters.py index 2815f6f6..bd3a011a 100644 --- a/Python/3. Longest Substring Without Repeating Characters.py +++ b/Python/Longest_Substring_Without_Repeating_Characters.py @@ -26,4 +26,4 @@ def lengthOfLongestSubstring(self, string: str) -> int: maxLength = max(maxLength, index-currentStartingIndex+1) # Assessing maxLength to be maximum of current substring with unique character and maximum length achieved at any point of time while carefully sliding the limits - return maxLength # Finally, returning the desired maximum length of contiguous substring that has no repeating characters \ No newline at end of file + return maxLength # Finally, returning the desired maximum length of contiguous substring that has no repeating characters From 172059c164fbb8e1624adcee9b5483bf63008862 Mon Sep 17 00:00:00 2001 From: Harshit Gupta Date: Mon, 2 Aug 2021 16:01:04 +0530 Subject: [PATCH 4/4] Fix Longest_Substring_Without_Repeating_Characters solution link Fixed the solution link for Longest_Substring_Without_Repeating_Characters.py file in README file --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ed5c166d..731afe53 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | # | Title | Solution | Time | Space | Difficulty | Tag | Note | | :--: | ----------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ------ | ------ | ---------- | --- | --------------- | -| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Python](./Python/3.%20Longest%20Substring%20Without%20Repeating%20Characters.py) | _O(n)_ | _O(n)_ | Medium | `Hash Table`
`Sliding Window` | Open for improvisation, mentioned time and space complexities unconfirmed | +| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Python](./Python/Longest_Substring_Without_Repeating_Characters.py) | _O(n)_ | _O(n)_ | Medium | `Hash Table`
`Sliding Window` | Open for improvisation, mentioned time and space complexities unconfirmed | | 8 | [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) | [Java](./Java/string-to-integer-atoi.java) | _O(n)_ | _O(1)_ | Medium | | | | 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [Java](./Java/palindrome-number.java) | _O(n)_ | _O(1)_ | Easy | | | | 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [Java](./Java/reverse-words-in-a-string.java) | _O(1)_ | _O(n)_ | Medium | | |