javascript - Given a string s, find the length of the longest substring without repeating characters - Stack Overflow

ExampleInput: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the

Example Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. I tried writing this but if condition is never being exceuted. I am not able to figure out the reason.

var lengthOfLongestSubstring = function(s) {
 let set = new Set();
    let c =0;
    for(let i =0; i< s.length; i++){
        if(set.has(s[i])){
            set.size =0;
        }
        else {
            console.log(c)
            c++;
        }
    }
    return c;
}; 
console.log(lengthOfLongestSubstring("abcabcbb"))

Example Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. I tried writing this but if condition is never being exceuted. I am not able to figure out the reason.

var lengthOfLongestSubstring = function(s) {
 let set = new Set();
    let c =0;
    for(let i =0; i< s.length; i++){
        if(set.has(s[i])){
            set.size =0;
        }
        else {
            console.log(c)
            c++;
        }
    }
    return c;
}; 
console.log(lengthOfLongestSubstring("abcabcbb"))

Share Improve this question edited Feb 3, 2021 at 3:27 10 Rep 2,2707 gold badges21 silver badges33 bronze badges asked Jan 30, 2021 at 11:09 sd_30sd_30 7081 gold badge11 silver badges26 bronze badges 2
  • oh the reason is simple.. u log new characters until there are no new ones.. that's y u only get abc.. if you did abcdefadefa it would return abcdef – The Bomb Squad Commented Jan 30, 2021 at 12:31
  • @Shilpi Were the answers helpful? – Shridhar R Kulkarni Commented Jan 31, 2021 at 5:31
Add a ment  | 

3 Answers 3

Reset to default 7

You can try this:

var lengthOfLongestSubstring = function (s) {
  let res = 0;
  let set = new Set();
  let i = 0;
  let j = 0;
  while (i < s.length && j < s.length) {
    if (!set.has(s[j])) {
      set.add(s[j]);
      j++;
      if (j - i > res) res = j - i;
    } else {
      set.delete(s[i]);
      i++;
    }
  }
  return res;
};

This is the same algorithm as described by danhuong, simply written with a recursive call and no mutable variables.

const longestSubstring = (str, i = 0, j = 0, found = new Set(), res = 0) =>
  j >= str.length
    ? res
  : found .has (str [j])
    ? longestSubstring (str, i + 1, j, found .delete (str [i]) && found, res)
    : longestSubstring (str, i, j + 1, found .add (str [j]), Math .max (res, j + 1 - i))

console .log (longestSubstring ("pwwkew"));
console .log (longestSubstring ("abcabcbb"));
console .log (longestSubstring ("abcabcbbvwxyz"));
console .log (longestSubstring ("abaca"));
console .log (longestSubstring ("abacdefg"));

You need to add the actual character.

set.size = 0 does not work. it is a read only property of Set.

Then you need to store the last found longest string and store it too.

Version with Set and without count, because Set has size.

const
  longestSubstring = function(s) {
      let set = new Set,
          longest = 0;

      for (const c of s) {
          if (set.has(c)) {
              if (longest < set.size) longest = set.size;
              set = new Set([c]);
          } else {
              set.add(c);
          }
      }
      if (longest < set.size) longest = set.size;
      return longest;
  };

console.log(longestSubstring("pwwkew"));
console.log(longestSubstring("abcabcbb"));
console.log(longestSubstring("abcabcbbvwxyz"));

A version with a string only.

const
  longestSubstring = function(s) {
      let sub = '',
          longest = 0;

      for (const c of s) {
          if (sub.includes(c)) {
              if (longest < sub.length) longest = sub.length;
              sub = c;
          } else {
              sub += c;
          }
      }
      if (longest < sub.length) longest = sub.length;
      return longest;
  };

console.log(longestSubstring("pwwkew"));
console.log(longestSubstring("abcabcbb"));
console.log(longestSubstring("abcabcbbvwxyz"));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744790434a4593880.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信