最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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"));

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论