ZhuangER
9/30/2018 - 3:48 AM

[3. Longest Substring Without Repeating Characters] #tags: leetcode

[3. Longest Substring Without Repeating Characters] #tags: leetcode

/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function(s) {
  let max = 0;
  
  for (let i = 0; i < s.length; i++) {
    for (let j = i + 1; j < s.length + 1; j++) {
      const tempStr = s.substring(i, j + 1)
      
      const isDuplicate = isStringDuplicate(tempStr);
      if (!isDuplicate && tempStr.length > max) {
        max = tempStr.length;
      }
    }
  }
  
  
  return max;
};

const isStringDuplicate = (str) => {
  const map = {};
  
  for (let i = 0; i < str.length; i++) {
    const char = str[i];
    
    if (map[char]) {
      return true;
    }
    
    map[char] = 1;
  }
  
  return false;
  
}