Longest Substring Without Repeating coding problem

I got stuck on this coding problem. I think that I can solve this coding problem by using regex. But I don’t know how to do that !!!

Here’s the problem:

Given a string, find the length of the longest substring without repeating characters

lengthOfLongestSubstring('abrkaabcdefghijjxxx')
// return 10

Could you help do this coding problem using regex?

What would your regex look for? Where would you start, where would you end? How do you know which end to shrink?

Pretty sure you’re going to need a better tool than regex.

There may be a better way to do it, but my first thought was to look at each character comparing it with the previous character.

That was my thinking too.

  1. Turn the ‘phrase’ into an array
  2. Loop over each item in the array in turn, comparing it to the previous member in turn
  3. When you hit a duplicate, concatenate the previous group into a single string variable, and push that into a new array
  4. When the end of the phrase is reached, check the new array for its longest member and output its length.

This is a bit rough, but I’m typing left-handed on an iPad with a cat sat on my lap.

1 Like

I have a quandry.

define repeating characters.
“aa”, definitely repeating.
“aba”. well, you repeated the a in the substring, but they’re not immediately repeating. is this string good or bad?
problem with the example is their answer is ambiguous. Change
lengthOfLongestSubstring('abrkaabcdefghijjxxx')
to be
lengthOfLongestSubstring('abrkaabcdafghijjxxx')
and it would remove the ambiguity…

(actually no it wouldnt, you’d need to replace it with “abrkaabcdfghaijjxxx” to make it unambiguous.)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.