The casual working described, to determine the length of the longest character string without duplication, we can use a queue; added sequentially to the characters in the queue, if the presence of the team, and vice versa, a team (until removal of repeated characters)
class Solution {
public intlengthOfLongestSubstring(String s){if(s.trim().isEmpty()&&(!s.isEmpty()))return1;if(s.isEmpty())return0;
Queue<Character> q = new LinkedList<Character>();int maxLength=1,length=1;
q.add(s.charAt(0));for(int i=1;i<s.length();i++){if(q.contains(s.charAt(i))){while(!q.element().equals(s.charAt(i))){
q.remove();
length--;}
q.remove();
length--;}
q.add(s.charAt(i));
length++;if(length>maxLength){
maxLength=length;}}return maxLength;}}
Official explanations
To traverse all possible substring given string s and call the function allUnique. If it turns out the return value is true, we will update the character substring no duplicate answers maximum length.
public class Solution {
public intlengthOfLongestSubstring(String s){int n = s.length();int ans =0;for(int i =0; i < n; i++)for(int j = i +1; j <= n; j++)if(allUnique(s, i, j)) ans = Math.max(ans, j - i);return ans;}
public boolean allUnique(String s,int start,int end){
Set<Character> set = new HashSet<>();for(int i = start; i < end; i++){
Character ch = s.charAt(i);if(set.contains(ch))return false;
set.add(ch);}return true;}}