Skip to content

Commit 658a615

Browse files
committed
Add minimum number of substrings without repeating chars
1 parent 267fa22 commit 658a615

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function minSubstringsWithoutRepeatingChar(str) {
2+
const seen = new Set();
3+
let countSubstrings = 0;
4+
5+
for(const char of str) {
6+
if(seen.has(char)) {
7+
// Duplicate found, start new substring
8+
countSubstrings++;
9+
seen.clear();
10+
}
11+
seen.add(char);
12+
}
13+
14+
return seen.size > 0 ? countSubstrings + 1 : countSubstrings;
15+
}
16+
17+
// Test cases
18+
console.log(minSubstringsWithoutRepeatingChar("abcabcbb")); // ➞ 4 ("abc", "a", "b", "cbb")
19+
console.log(minSubstringsWithoutRepeatingChar("bbbbb")); // ➞ 5 ("b", "b", "b", "b", "b")
20+
console.log(minSubstringsWithoutRepeatingChar("pwwkew")); // ➞ 3 ("pw", "wk", "ew")
21+
console.log(minSubstringsWithoutRepeatingChar("")); // ➞ 0
22+
console.log(minSubstringsWithoutRepeatingChar("a")); // ➞ 1
23+
console.log(minSubstringsWithoutRepeatingChar("abacabadabacaba")); // ➞ 8
24+

0 commit comments

Comments
 (0)