DEV Community

Bvnkumar
Bvnkumar

Posted on

Longest string without repeating characters in javaascript

 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function(s) {
  let map = new Map();
    if (!s.length) {
        return null
    }
    for (let i = 0; i < s.length; i++) {
        let count = 0;
        let checkString = s.substring(0, i + 1)
        let start = 0;
        while (s.substring(start, s.length - 1).indexOf(checkString) > -1) {
            count++;
            if (map.has(checkString)) {
                let existedCount = map.get(checkString);
                map.set(checkString, existedCount + 1)
                existedCount = 0;
            } else {
                map.set(checkString, count)
            }
            start++;
        }
    }
    if (map.size) {
        let keys = Array.from(map.keys());
        return keys.reduce((a, b) => map.get(a) > map.get(b) ? map.get(a) : map.get(b))
    }
};
//console.log("Higest", lengthOfLongestSubstring('abcabcbb'))
//console.log("map", map)`
Enter fullscreen mode Exit fullscreen mode

Any suggestions or comments are welcome.

Top comments (0)