1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| package com.demo.s3;
import java.util.HashMap; import java.util.Map;
public class Solution {
public int lengthOfLongestSubstring(String s) { char[] chars = s.toCharArray(); int start = 0; int max = 0; Map<Character, Integer> cs = new HashMap<>(); for(int i= 0; i < chars.length; i++) { Integer lastExist = cs.get(chars[i]); if(lastExist!=null && lastExist >= start) { start = lastExist + 1; } else if(i - start + 1 > max) { max = i - start + 1; } cs.put(chars[i], i); } return max; }
public static void main(String[] args) {
} }
|