Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
LeetCode/longest-substring-without-repeating-characters
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
49 lines (39 sloc)
1.1 KB
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
Given a string, find the length of the longest substring without repeating characters. | |
Example 1: | |
Input: "abcabcbb" | |
Output: 3 | |
Explanation: The answer is "abc", with the length of 3. | |
Example 2: | |
Input: "bbbbb" | |
Output: 1 | |
Explanation: The answer is "b", with the length of 1. | |
Example 3: | |
Input: "pwwkew" | |
Output: 3 | |
Explanation: The answer is "wke", with the length of 3. | |
Note that the answer must be a su | |
My code: | |
class Solution: | |
def lengthOfLongestSubstring(self, s): | |
""" | |
:type s: str | |
:rtype: int | |
""" | |
length=len(s) | |
tmp_dict={} | |
tmp_len=length | |
start=0 | |
result=0 | |
i=0 | |
while i<length: | |
if s[i] not in tmp_dict.keys(): | |
tmp_dict[s[i]]=1 | |
i=i+1 | |
if result<i-start: | |
result=i-start | |
else: | |
tmp_dict.pop(s[start]) | |
start+=1 | |
return result | |
Solution: | |
https://leetcode.com/problems/longest-substring-without-repeating-characters/ |