Longest Substring Without Repeating Characters Flashcards

https://leetcode.com/problems/longest-substring-without-repeating-characters/?envType=list&envId=rkzwnm95

1
Q

How will you store char?

A

new Set()
- Use Set which is an object that lets you store unique values of any type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What variables will you use?

A
  • Use start = 0 as pointer
  • Use longest = 0 to keep track of longest string
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How will you loop?

A
  • Use for loop and set “end” pointer as variable
  • Use while loop to check if set has char
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What happens if set has char?

A
  • Delete char from start pointer
  • Increment start
  • Add new char from end pointer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How is longest constantly being checked/updated?

A
  • Reassigning longest through Math.max comparing current longest and pointers end - start + 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the time complexity?

A

O(n)
- Even though there is a nested while loop, each character in string is processed only once to add and remove from set
-The overall number of operations is proportional to n, the length of the string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the space complexity?

A

O(n)
- Worst-case scenario all the characters are unique, so the set will have to store all n characters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly