Skip to content

Commit 76b0f81

Browse files
authored
Optimal Partition of String (#357)
Optimal Partition of String
1 parent 1bbe8c4 commit 76b0f81

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int partitionString(String s) {
3+
int ans = 1;
4+
HashSet<Character> st = new HashSet<>();
5+
for(int i=0;i<s.length();i++){
6+
// Insert Till we find duplicate element.
7+
if(!st.contains(s.charAt(i))){
8+
st.add(s.charAt(i));
9+
}
10+
else{
11+
// If we found duplicate char then increment count and clear set and start with new set.
12+
ans++;
13+
st.clear();
14+
st.add(s.charAt(i));
15+
}
16+
}
17+
return ans;
18+
}
19+
}

0 commit comments

Comments
 (0)