-
-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution for 3rd LeetCode Practice Problem - Longest Substring Wi…
…thout 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
- Loading branch information
1 parent
cc22876
commit c046825
Showing
2 changed files
with
37 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters