Longest Substring Without Repeating Characters
Total Accepted: 159876
Total Submissions: 710693
Difficulty: Medium
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb"
, the answer is "abc"
, which the length is 3.
Given "bbbbb"
, the answer is "b"
, with the length of 1.
Given "pwwkew"
, the answer is "wke"
, with the length of 3. Note that the answer must be a substring, "pwke"
is a subsequence and not a substring.
基本上没有什么技术含量,使用set就能简单的解决:
Python:
#Python:
class Solution:
def longestSubString(self,s):
subnum=len(set(s))
return subnum
if __name__=="__main__":
print Solution().longestSubString("yboyubasfbh")
Java:
Java:
public class Solution{
public int longestSubString(String s){
Set<Character> set=new HashSet<Character>();
for(int i=0;i<s.length;i++){
set.add(s.CharAt(i));
}
return set.size();
}
}