DEV Community

Bvnkumar
Bvnkumar

Posted on

1 1

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.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay