CodeNewbie Community

Abhilasha Kumari
Abhilasha Kumari

Posted on

Longest Substring Without Repeating Characters

testcase not passed= "pwwkew"

var lengthOfLongestSubstring = function(s) {

var ans=0
var i=-1
var j=-1

var obj={}

while(true){

    var f1=false
    var f2=false

    //acquire

    while(i<s.length-1){
         f1=true
          i++
        if(obj[s[i]]){
            obj[s[i]]+=1
        }else{
            obj[s[i]]=1
        }
        if(obj[s[i]]==2){
            break;
        }else{
            var len=i-j
            if(len>ans){

                ans=len
            }
        }
    }



    //release

    while(j<i){
        f2=true
        j++
        obj[s[i]]+=-1

        if(obj[s[i]]==1){
            break;
        }

    }

    if(f1==false && f2==false){
        break
    }
}

return ans
Enter fullscreen mode Exit fullscreen mode

};

Discussion (0)