How to find the longest substring without duplicates in characters - Python solution

Search for a command to run...

Automating the boring stuff in job applications, while keeping the final decision in your hands.

Automating the boring stuffs ...

Managing S3 Case Sensitivity in Python Workflows

π©οΈ Cloud computing has revolutionized how applications are developed and deployed, providing a more flexible and scalable environment for running workloads. AWS offers several services for event-driven computing, including Amazon CloudWatch Events a...

Introductionπ Looking to streamline your complex workflows and keep everything on track? Look no further than state machines - a powerful tool for precision and ease in process management. πͺπΌ And when it comes to implementing state machines, AWS S...

Peterson Oaikhenah -- Senior Software Engineer | Community leader | Cloud Engineer | Open Source
38 posts
Motivated senior software engineer, open-source advocate & community leader π»β‘οΈ. Sharing valuable insights to help others scale π‘. Building strong communities π. Passionate about promoting tech
Photo by Sebastian Herrmann on Unsplash
Coding challenges can be head-scratching most of the time π. Well, for the most part, they require thorough understanding of the problem. This might involve formulating a use case and manually working through the problem with some sets of sample data.
The problem above says we should find the length of the longest sub-string without repeating characters or duplicates π€ .
So, if we have "pwwkew", the non-repeating sub-strings include "pw", "wke" and "w", but the longest sub-string without duplicating characters is "wke". This is because the moment we move to the next character after "e", we hit another "w" and remember, we already have hit a previous w.
We thus need to keep track of the length of a sub-string, when it hits a duplicate and continually update the starting point of other sub-strings.
Let's implement this in code π€:
class Solution(object):
def lengthOfLongestSubstring(self, string: str) -> int:
sub = {} #define a hashmap track every char and its current index
cur_sub_start = 0 #this variable keeps track of the current starting index of a sub-string
cur_len = 0 #when the hashmap is updated or the current sub-string meets a duplicate, the current length of the substring is updated
longest = 0 #this variable records the max length of the cur_len of a sub-string
#this loop traverses the input string , checking if a char exists in the hash table
for i, letter in enumerate(string):
if letter in sub and sub[letter] >= cur_sub_start:
#this condition checks if the char is in the hash table
#and if the char exists in the current substring
cur_sub_start = sub[letter] + 1
# increment the current substring starting index, because it's met a duplicate
cur_len = i - sub[letter] #the current index - the index in the hash table,
#gives updates the current length of the substring
sub[letter] = i
# update the hash table with the current index of the duplicate char
else:
sub[letter] = i
# if the char is not in the hash table add it.
cur_len += 1 #increment the current sub-string length since it was updated
if cur_len > longest:
# if the current sub-string length is greater than the previous length, update it
longest = cur_len
return(longest) #returns the longest string
With our solution we eliminate the need to write nested loops, or to traverse the starting and ending sub-string indexes. We don't also need to first get all possible sub-strings π, before implementing our criteria in code.
According to leetcode:
Longest Substring Without Repeating Characters.
Runtime: 48 ms, faster than 96.97% of Python3 online submissions for Longest Substring Without Repeating Characters. Memory Usage: 14.1 MB, less than 17.66% of Python3 online submissions for Longest Substring Without Repeating Characters.
Is this the most optimized solution? Certainly not, there are other approaches to this problem . If you have a better solution or you'll like to improve my code, it's here π . Please feel free to leave a comment, like and share.
Thanks for the audience π€. connect with me on Github, linkedIn && Twitter.