Simple Java using HashSet


  • 0
    A
    public class Solution {
    public int lengthOfLongestSubstring(String s) {
        HashSet<Character> hash = new HashSet<Character>();
        int n = s.length();
        int max = 0;
        for(int i = 0; i < n; i++){
        	int j = i;
        	int an = 0;
        	while(j < n&&hash.add(s.charAt(j))){
        		j++;
        		an++;
        	}
        	max = Math.max(max, an);
        	hash.clear();
        }
        return max;
    }
    

    }


Log in to reply
 

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