You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add solution for 3rd LeetCode Practice Problem - Longest Substring Without Repeating Characters (#184)
* 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).
* 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
* 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
* Fix Longest_Substring_Without_Repeating_Characters solution link
Fixed the solution link for Longest_Substring_Without_Repeating_Characters.py file in README file
# Creating a charactersCountDict to store count of characters in the current
11
+
charactersCountDict= {}
12
+
13
+
# declaring variables to mark the Starting Index as well as Maximum Length of any contiguous substring without recurring characters achieved
14
+
currentStartingIndex=maxLength=0
15
+
16
+
# Iterating through all indices of the string, one by one, while analyzing string between 'currentStartingIndex' and this ending 'index'.
17
+
forindexinrange(len(string)):
18
+
# 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
19
+
whilestring[index] incharactersCountDict:
20
+
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)
21
+
ifcharactersCountDict[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
22
+
currentStartingIndex+=1# Shifting the currentStartingIndex one step ahead
23
+
24
+
# 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)
25
+
charactersCountDict[string[index]] =1
26
+
27
+
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
28
+
29
+
returnmaxLength# Finally, returning the desired maximum length of contiguous substring that has no repeating characters
| 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`<br/>`Sliding Window`| Open for improvisation, mentioned time and space complexities unconfirmed |
161
+
| 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 |||
| 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 |||
160
164
| 383 |[Ransom Note](https://leetcode.com/problems/ransom-note/)|[Java](./Java/ransom-note.java)|_O(1)_|_O(n)_| Easy || Character Count |
161
165
| 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 |
162
-
| 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 |||
163
166
| 520 |[Detect Capital Use](https://leetcode.com/problems/detect-capital/)|[Java](./Java/detect-capital-use.java)|_O(n)_|_O(1)_| Easy |||
167
+
| 767 |[Reorganize String](https://leetcode.com/problems/reorganize-string/)|[Python](./Python/reorganize-string.py)|_O(n)_|_O(n)_| Medium |||
| 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 |||
165
-
| 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 |||
166
170
| 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 |||
| 767 |[Reorganize String](https://leetcode.com/problems/reorganize-string/)|[Python](./Python/reorganize-string.py)|_O(n)_|_O(n)_| Medium |||
170
-
| 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 |||
171
+
| 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 |||
0 commit comments