DEV Community

Paolo Ventura
Paolo Ventura

Posted on • Updated on

100 algos in 100 days (Day 9)

Substring with the most unique, consecutive letters

(https://leetcode.com/problems/longest-substring-without-repeating-characters/)

My thought process:

How to get all substrings?
Short strings:

1 = 1 substring
2 = 2 + 1 substrings
3 = 3 + 2 + 1 substrings

'aba'
You look at the 3 ... Nope... Take 1 letter off each side

'abc'
You look at the 3.. nope...

'abagh'

Better approach

// Make a set with uniques
// We know the highest possible length e.g. 3 uniques in 3 letter string is 1. This could be max depth.
// Loop through if has char.. add one to highest so far
// If already has that char reset
// If doesn't have reset current sequence to 1 consecutive unique digit. Update highest

What about as you go through, check the set... If not reset just set it equal to used: false

Is set the best data structure to use? Or could we use a JS object to better effect?

As we loop we use a variable to check the highest so far.

Top comments (0)