Split a string into maximum number of unique substrings
Given string str, the task is to split the string into the maximum number of unique substrings possible and print their count.
Examples:
Input: str = “ababccc”
Output: 5
Explanation:
Split the given string into the substrings “a”, “b”, “ab”, “c” and “cc”.
Therefore, the maximum count of unique substrings is 5.
Input: str = “aba”
Output: 2
Approach: The problem can be solved by the Greedy approach. Follow the steps below to solve the problem:
- Initialize a Set S.
- Iterate over the characters of the string str and for each i and find the substring up to that index.
- If the given substring is not present in the Set S, insert it updates the maximum count, and remove it from the Set, because the same character cannot be reused.
- Return the maximum count.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int maxUnique(string S, set<string> st)
{
int mx = 0;
for ( int i = 1; i <= S.length(); i++)
{
string tmp = S.substr(0, i);
if (st.find(tmp) == st.end())
{
st.insert(tmp);
mx = max(mx, maxUnique(S.substr(i), st) + 1);
st.erase(tmp);
}
}
return mx;
}
int maxUniqueSplit(string S)
{
set<string> st;
return maxUnique(S, st);
}
int main()
{
string S = "ababccc" ;
int res = maxUniqueSplit(S);
cout<<res;
}
|
Java
import java.io.*;
import java.util.*;
class Solution {
public int maxUniqueSplit(String S)
{
return maxUnique(S, new HashSet<String>());
}
public int maxUnique(String S, Set<String> set)
{
int max = 0 ;
for ( int i = 1 ; i <= S.length(); i++) {
String tmp = S.substring( 0 , i);
if (!set.contains(tmp)) {
set.add(tmp);
max = Math.max(max, maxUnique(
S.substring(i), set)
+ 1 );
set.remove(tmp);
}
}
return max;
}
}
class GFG {
public static void main(String[] args)
{
Solution st = new Solution();
String S = "ababccc" ;
int res = st.maxUniqueSplit(S);
System.out.println(res);
}
}
|
Python3
def maxUnique(S):
global d
maxm = 0
for i in range ( 1 , len (S) + 1 ):
tmp = S[ 0 :i]
if (tmp not in d):
d[tmp] = 1
maxm = max (maxm, maxUnique(S[i:]) + 1 )
del d[tmp]
return maxm
if __name__ = = '__main__' :
S = "ababccc"
d = {}
res = maxUnique(S)
print (res)
|
C#
using System;
using System.Collections.Generic;
class GFG {
public int maxUniqueSplit(String S)
{
return maxUnique(S, new HashSet<String>());
}
public int maxUnique(String S, HashSet<String> set )
{
int max = 0;
for ( int i = 1; i <= S.Length; i++) {
String tmp = S.Substring(0, i);
if (! set .Contains(tmp)) {
set .Add(tmp);
max = Math.Max(max, maxUnique(
S.Substring(i), set )
+ 1);
set .Remove(tmp);
}
}
return max;
}
}
public class GFG {
public static void Main(String[] args)
{
Solution st = new Solution();
String S = "ababccc" ;
int res = st.maxUniqueSplit(S);
Console.WriteLine(res);
}
}
|
Javascript
<script>
function maxUnique(S, st)
{
var mx = 0;
for ( var i = 1; i <= S.length; i++)
{
var tmp = S.substring(0, i);
if (!st.has(tmp))
{
st.add(tmp);
mx = Math.max(mx, maxUnique(S.substring(i), st) + 1);
st. delete (tmp);
}
}
return mx;
}
function maxUniqueSplit(S)
{
var st = new Set();
return maxUnique(S, st);
}
var S = "ababccc" ;
var res = maxUniqueSplit(S);
document.write( res);
</script>
|
Time Complexity: O()
Auxiliary Space: O(N)
Last Updated :
18 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...