Simple java solution only String


  • 2
    C
    public class Solution {
        public int lengthOfLongestSubstring(String s) {
            int len = 0;
            int head = 0;
            int tmpl = 0;
            String sss = "";
            for (int i = 0; i < s.length(); i++) {
    			if(sss.indexOf(s.charAt(i)) != -1){
    				head += sss.indexOf(s.charAt(i))+1;
                    sss = s.substring(head, i);
                    tmpl = sss.length();
    			}
    			tmpl++;
    			sss += s.charAt(i);
    			len = Math.max(len, tmpl);
    		}
        		return len;
        }
    }

Log in to reply
 

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