牛牛非常怕他的女朋友,怕到了走火入魔的程度,以至于每当他看到一个字符串同时含有n,p,y三个字母他都害怕的不行。现在有一个长度为m的只包含小写字母‘a’-‘z’的字符串x,牛牛想知道能令他不害怕的最长子串的长度是多少。(对于字符串”abc”来说,”c”,”ab”都是原串的子串,但”ac”不是原串子串)
function Maximumlength( x ) {
// write code here
let window = new Map();
let left = 0;
let right = 0;
let len = 0;
// console.log(x.length);
while(right < x.length) {
let c = x.charAt(right);
if(window.has(c)) {
window.set(c, window.get(c) + 1);
} else {
window.set(c,1);
}
right++;
while(c = ('n' || 'p' || 'y') && window.get('n') >=1 && window.get('p') >=1 && window.get('y') >=1 ) {
let d = x.charAt(left);
left++;
window.set(d, window.get(d) - 1);
// if((right - left) > len) {
// len = Math.max(len, right - left);
// }
}
len = Math.max(len, right - left);
}
// console.log(left);
// console.log(right);
return len;
}
console.log(Maximumlength('ypknnbpiyc'));