📌  相关文章
📜  将字符串拆分为最大数目的唯一子字符串

📅  最后修改于: 2021-04-17 13:52:56             🧑  作者: Mango

给定字符串str ,任务是将字符串分成最大数量的唯一子字符串,并打印其计数。

例子:

方法:该问题可以通过贪婪方法解决。请按照以下步骤解决问题:

  1. 初始化集合S。
  2. 遍历字符串str和每个i的字符,并找到直至该索引的子字符串。
  3. 如果Set S中不存在给定的子字符串,请插入该子字符串并更新最大计数,然后将其从Set中删除,因为无法重复使用同一字符。
  4. 返回最大计数。

下面是上述方法的实现:

C++
// CPP program for the above approach
#include
using namespace std;
 
// Utility function to find maximum count of
// unique substrings by splitting the string
int maxUnique(string S, set st)
{
 
  // Stores maximum count of unique substring
  // by splitting the string into substrings
  int mx = 0;
 
  // Iterate over the characters of the string
  for (int i = 1; i <= S.length(); i++)
  {
 
    // Stores prefix substring
    string tmp = S.substr(0, i);
 
    // Check if the current substring
    // already exists
    if (st.find(tmp) == st.end())
    {
 
      // Insert tmp into set
      st.insert(tmp);
 
      // Recursively call for remaining
      // characters of string
      mx = max(mx, maxUnique(S.substr(i), st) + 1);
 
      // Remove from the set
      st.erase(tmp);
    }
  }
 
  // Return answer
  return mx;
}
 
 
// Function to find the maximum count of
// unique substrings by splitting a string
// into maximum number of unique substrings
int maxUniqueSplit(string S)
{
  set st;
  return maxUnique(S, st);
}
 
// Driver Code
int main()
{
  string S = "ababccc";
  int res = maxUniqueSplit(S);
 
  cout<


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class Solution {
 
    // Function to find the maximum count of
    // unique substrings by splitting a string
    // into maximum number of unique substrings
    public int maxUniqueSplit(String S)
    {
        return maxUnique(S, new HashSet());
    }
 
    // Utility function to find maximum count of
    // unique substrings by splitting the string
    public int maxUnique(String S, Set set)
    {
 
        // Stores maximum count of unique substring
        // by splitting the string into substrings
        int max = 0;
 
        // Iterate over the characters of the string
        for (int i = 1; i <= S.length(); i++) {
 
            // Stores prefix substring
            String tmp = S.substring(0, i);
 
            // Check if the current substring
            // already exists
            if (!set.contains(tmp)) {
 
                // Insert tmp into set
                set.add(tmp);
 
                // Recursively call for remaining
                // characters of string
                max = Math.max(max, maxUnique(
                                        S.substring(i), set)
                                        + 1);
 
                // Remove from the set
                set.remove(tmp);
            }
        }
 
        // Return answer
        return max;
    }
}
 
// Driver Code
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
# Python3 program for the above approach
 
# Utility function to find maximum count of
# unique substrings by splitting the string
def maxUnique(S):
    global d
 
    # Stores maximum count of unique substring
    # by splitting the string into substrings
    maxm = 0
 
    # Iterate over the characters of the string
    for i in range(1, len(S) + 1):
 
        # Stores prefix substring
        tmp = S[0:i]
 
        # Check if the current substring
        # already exists
        if (tmp not in d):
 
            # Insert tmp into set
            d[tmp] = 1
 
            # Recursively call for remaining
            # characters of string
            maxm = max(maxm, maxUnique(S[i:]) + 1)
 
            # Remove from the set
            del d[tmp]
 
    # Return answer
    return maxm
 
# Driver Code
if __name__ == '__main__':
   
    # Solution st = new Solution()
    S = "ababccc"
    d = {}
    res = maxUnique(S)
    # d = {}
 
    print(res)
 
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG {
 
  // Function to find the maximum count of
  // unique substrings by splitting a string
  // into maximum number of unique substrings
  public int maxUniqueSplit(String S)
  {
    return maxUnique(S, new HashSet());
  }
 
  // Utility function to find maximum count of
  // unique substrings by splitting the string
  public int maxUnique(String S, HashSet set)
  {
 
    // Stores maximum count of unique substring
    // by splitting the string into substrings
    int max = 0;
 
    // Iterate over the characters of the string
    for (int i = 1; i <= S.Length; i++) {
 
      // Stores prefix substring
      String tmp = S.Substring(0, i);
 
      // Check if the current substring
      // already exists
      if (!set.Contains(tmp)) {
 
        // Insert tmp into set
        set.Add(tmp);
 
        // Recursively call for remaining
        // characters of string
        max = Math.Max(max, maxUnique(
          S.Substring(i), set)
                       + 1);
 
        // Remove from the set
        set.Remove(tmp);
      }
    }
 
    // Return answer
    return max;
  }
}
 
// Driver Code
public class GFG {
 
  public static void Main(String[] args)
  {
    Solution st = new Solution();
    String S = "ababccc";
    int res = st.maxUniqueSplit(S);
 
    Console.WriteLine(res);
  }
}
 
// This code contributed by shikhasingrajput


输出:
5

时间复杂度: O(N)
辅助空间: O(N)