Simple Python Solution with one looping statement


  • 0
    S
    class Solution:
    	# @return an integer
    	def lengthOfLongestSubstring(self, s):
    		tempString = []
    		lenString = {}
    		if s == "":
    			return 0
    		for j in range(0, len(s)):
    			if s[j] in tempString:
    				lenString[('').join(tempString)] = len(('').join(tempString))
    				tempString = tempString[tempString.index(s[j])+1:len(tempString)]
    				tempString.append(s[j])
    			else:
    				tempString.append(s[j])
    			if j == len(s) - 1:
    				lenString[('').join(tempString)] = len(('').join(tempString))
    		if len(lenString) == 0:
    			return 0
    		return max(sorted(i for i in lenString.values()))
    

    This is a very crude solution since I am very amateur to python programming. Hope this solution helps!!!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.