substring way


  • 0
    Y

    class Solution {
    public int lengthOfLongestSubstring(String s) {
    int begin=0,offset=1,len=0,end=1;
    if(s.length() == 1 ){
    return 1;
    } else if(s.length() == 0) {
    return 0;
    } else {
    while (end<s.length()) {
    String sb = s.substring(begin,end);
    String ch = s.charAt(end)+"";
    if(!sb.contains(ch)){
    offset = offset+1;
    } else {
    if(offset>len){
    len = offset;
    }
    begin = s.indexOf(ch,begin)+1;
    offset = offset - sb.indexOf(ch,0);
    }
    end = begin+offset;
    }
    if(offset > len) {
    len = offset;
    }
    }
    return len;
    }
    }


Log in to reply
 

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