Skip to content

Commit 1e192e6

Browse files
author
hasibulislam999
committed
Optimal Partition of String problem solved
1 parent 8c11531 commit 1e192e6

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Title: Optimal Partition of String
3+
* Description: Given a string s, partition the string into one or more substrings such that the characters in each substring are unique. That is, no letter appears in a single substring more than once.
4+
* Author: Hasibul Islam
5+
* Date: 04/04/2023
6+
*/
7+
8+
/**
9+
* @param {string} s
10+
* @return {number}
11+
*/
12+
var partitionString = function (s) {
13+
if (s.length === 0) return 0;
14+
15+
let map = {},
16+
cur = 1;
17+
map[cur] = new Set();
18+
19+
for (let i = 0; i < s.length; i++) {
20+
let char = s[i];
21+
22+
if (map[cur].has(char)) {
23+
cur++;
24+
map[cur] = new Set();
25+
}
26+
map[cur].add(char);
27+
}
28+
29+
return cur;
30+
};
31+
32+
console.log(partitionString("aab"));

0 commit comments

Comments
 (0)