Strings • Sets • Sliding Window • Beginner Developer Journey
I realized that learning syntax alone doesn’t make someone a programmer — real programming is about thinking, logic, and solving problems.
That’s why I’m starting this Problem Solving Series, inspired by a conversation with my friend Bala yesterday (you can read that story here:https://dev.to/buddingdeveloper/i-blinked-when-my-friend-asked-this-question-39k4
Problem Solving Series
Every day I will pick one problem, try to understand it, think about the logic, and share the learning.Some problems will be easy.Some will be confusing.Some might break my brain.
But the goal is simple:
Learn to think like a programmer, not just write code.
And today is Day 1.
Today’s Problem
The problem looks simple at first glance.
You are given a string.
Your task is to find the longest substring that contains no repeating characters.
Example string:
pwwkew
We need to find the longest continuous group of characters where no character repeats.
Possible substrings:
pw
wk
wke
kew
The longest one is:
wke
Length:
3
So the output should tell us:
the length
and the actual substring
Thinking Before Coding
Instead of jumping directly into code, I tried to think about the logic first.
Imagine reading the string from left to right.
We keep a window of characters that are currently unique.
Example:
p → unique
pw → still unique
Then suddenly we see:
pww
Now a repetition appears.So the window is no longer valid.
We need to move the starting point forward until the repetition disappears.This idea is known as the Sliding Window Technique.
Many real interview problems use this pattern.
Why I Used a Set
While solving the problem I needed a quick way to answer this question:
“Have I already seen this character in my current window?”
JavaScript has a very nice data structure for this.
It’s called a Set.
A Set is a collection that stores only unique values.
For example:
const seen = new Set()
seen.add("a")
seen.add("b")
seen.add("a")
console.log(seen)
Output:
Set { 'a', 'b' }
Even though "a" was added twice, the Set keeps only one copy.
This makes it perfect for tracking unique characters in a substring.
The Set gives us three very useful operations:
Extracting the Longest Substring
At the end of the algorithm we know two important things:
where the longest substring startshow long it isNow we need to cut that part from the string.
JavaScript gives us a simple method called:
substring()
Example:
let word = "javascript"
console.log(word.substring(0,4))
Output:
java
It extracts characters starting from index 0 up to 4 (not including 4).
In our solution we use this idea like this:
str.substring(startIndex, startIndex + length)
This gives us the exact longest substring.
JavaScript Solution
function longestUniqueSubstring(str) {
let start = 0
let maxLength = 0
let maxStart = 0
const seen = new Set()
for (let end = 0; end < str.length; end++) {
while (seen.has(str[end])) {
seen.delete(str[start])
start++
}
seen.add(str[end])
if (end - start + 1 > maxLength) {
maxLength = end - start + 1
maxStart = start
}
}
return {
length: maxLength,
substring: str.substring(maxStart, maxStart + maxLength)
}
}
Why This Solution Is Efficient
This algorithm scans the string only once.
So the time complexity (TBD)is:
O(n)
Which means it scales well even for long strings.
Instead of checking every possible substring (which would be slow), we expand and shrink a window dynamically.
That’s the power of the sliding window technique.
What I Learned Today
Day 1 of this series already taught me a few important things.
Programming problems are not about writing complicated code. They are about understanding patterns and logic.
Today’s problem helped me learn about:
- The Sliding Window technique
- How Sets store unique values
- How to extract parts of strings using substring()
And most importantly, it reminded me that becoming a developer is not about rushing through tutorials.
It’s about thinking, struggling, and gradually improving.
That’s it for Problem Solving — Day 1
If you solved this problem in a different way, I’d love to see your approach.
Share it in the comments — I’m always excited to learn new ways of thinking.
See you tomorrow with the next problem… and hopefully a slightly smarter brain. 😄
Top comments (0)