My Easy Java solution with constant space and O(n) time

  • 0
    public class Solution {
        public int lengthOfLongestSubstring(String s) {
            int[] map = new int[256];
            int maxLen = 0, start=0, end=-1;
            for(int i=0; i<s.length();i++){
                int index  = s.charAt(i);
                    maxLen = Math.max(maxLen, end-start+1);
                    start = Math.max(map[index], start);
                map[index] = i+1;
            return Math.max(maxLen, end-start+1);

Log in to reply

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