📌  相关文章
📜  最大化分区,使得没有两个子字符串具有任何公共字符

📅  最后修改于: 2021-10-25 09:18:32             🧑  作者: Mango

给定大小为N 的字符串str ,任务是打印在最大可能分区后形成的子字符串数,以便没有两个子字符串具有公共字符。
例子:

方法:

  1. 从字符串的末尾找到每个唯一字符的最后一个索引并将其存储在映射中。
  2. 遍历数组从索引 0 到最后一个索引,并创建一个单独的变量来存储分区的结束索引。
  3. 遍历字符串中的每个变量并检查分区的末尾(由存储在映射中的str[i]的索引表示)是否大于前一个末尾。如果是这样,请更新它。
  4. 检查当前变量是否超过分区的末尾。这意味着找到了第一个分区。将分区的末尾更新为max(k, mp[str[i]])
  5. 遍历整个字符串并使用类似的过程找到下一个分区等等。

下面是上述方法的实现:

C++
// C++ program to find the
// maximum number of
// partitions possible such
// that no two substrings
// have common character
 
#include 
using namespace std;
 
// Function to calculate and return
// the maximum number of partitions
int maximum_partition(string str)
{
    // r: Stores the maximum number
    // of partitions
    // k: Stores the ending index
    // of the partition
    int i = 0, j = 0, k = 0;
    int c = 0, r = 0;
 
    // Stores the last index
    // of every unique character
    // of the string
    unordered_map m;
 
    // Traverse the string and
    // store the last index
    // of every character
    for (i = str.length() - 1;
        i >= 0;
        i--) {
 
        if (m[str[i]] == 0) {
            m[str[i]] = i;
        }
    }
 
    i = 0;
 
    // Store the last index of the
    // first character from map
    k = m[str[i]];
 
    for (i = 0; i < str.length(); i++) {
 
        if (i <= k) {
            c = c + 1;
 
            // Update K to find
            // the end of partition
            k = max(k, m[str[i]]);
        }
 
        // Otherwise, the end of
        // partition is found
        else {
 
            // Increment r
            r = r + 1;
            c = 1;
 
            // Update k for the
            // next partition
            k = max(k, m[str[i]]);
        }
    }
 
    // Add the last partition
    if (c != 0) {
        r = r + 1;
    }
    return r;
}
 
// Driver Program
int main()
{
    string str
        = "ababcbacadefegdehijhklij";
    cout << maximum_partition(str);
}


Java
// Java program to find the
// maximum number of
// partitions possible such
// that no two subStrings
// have common character
import java.util.*;
class GFG{
 
// Function to calculate and return
// the maximum number of partitions
static int maximum_partition(String str)
{
  // r: Stores the maximum number
  // of partitions
  // k: Stores the ending index
  // of the partition
  int i = 0, j = 0, k = 0;
  int c = 0, r = 0;
 
  // Stores the last index
  // of every unique character
  // of the String
  HashMap m = new HashMap<>();
 
  // Traverse the String and
  // store the last index
  // of every character
  for (i = str.length() - 1;
       i >= 0; i--)
  {
    if (!m.containsKey(str.charAt(i)))
    {
      m.put(str.charAt(i), i);
    }
  }
 
  i = 0;
 
  // Store the last index of the
  // first character from map
  k = m.get(str.charAt(i));
 
  for (i = 0; i < str.length(); i++)
  {
    if (i <= k)
    {
      c = c + 1;
 
      // Update K to find
      // the end of partition
      k = Math.max(k, m.get(str.charAt(i)));
    }
 
    // Otherwise, the end of
    // partition is found
    else
    {
      // Increment r
      r = r + 1;
      c = 1;
 
      // Update k for the
      // next partition
      k = Math.max(k, m.get(str.charAt(i)));
    }
  }
 
  // Add the last
  // partition
  if (c != 0)
  {
    r = r + 1;
  }
  return r;
}
 
// Driver code
public static void main(String[] args)
{
  String str = "ababcbacadefegdehijhklij";
  System.out.print(maximum_partition(str));
}
}
 
// This code is contributed by Princi Singh


Python 3
# Python3 program to find the
# maximum number of
# partitions possible such
# that no two substrings
# have common character
 
# Function to calculate and return
# the maximum number of partitions
def maximum_partition(strr):
     
    # r: Stores the maximum number
    # of partitions
    # k: Stores the ending index
    # of the partition
    i = 0
    j = 0
    k = 0
    c = 0
    r = 0
 
    # Stores the last index
    # of every unique character
    # of the string
    m = {}
 
    # Traverse the and
    # store the last index
    # of every character
    for i in range(len(strr) - 1, -1, -1):
 
        if (strr[i] not in m):
            m[strr[i]] = i
 
    i = 0
 
    # Store the last index of the
    # first character from map
    k = m[strr[i]]
 
    for i in range(len(strr)):
 
        if (i <= k):
            c = c + 1
             
            # Update K to find
            # the end of partition
            k = max(k, m[strr[i]])
 
        # Otherwise, the end of
        # partition is found
        else:
 
            # Increment r
            r = r + 1
            c = 1
 
            # Update k for the
            # next partition
            k = max(k, m[strr[i]])
 
    # Add the last partition
    if (c != 0):
        r = r + 1
 
    return r
 
# Driver Code
if __name__ == '__main__':
    strr = "ababcbacadefegdehijhklij"
    print(maximum_partition(strr))
 
# This code is contributed by Mohit Kumar


C#
// C# program to find the
// maximum number of
// partitions possible such
// that no two subStrings
// have common character
using System;
using System.Collections.Generic;
class GFG{
 
// Function to calculate and return
// the maximum number of partitions
static int maximum_partition(String str)
{
  // r: Stores the maximum number
  // of partitions
  // k: Stores the ending index
  // of the partition
  int i = 0, j = 0, k = 0;
  int c = 0, r = 0;
 
  // Stores the last index
  // of every unique character
  // of the String
  Dictionary m = new Dictionary();
 
  // Traverse the String and
  // store the last index
  // of every character
  for (i = str.Length - 1;
       i >= 0; i--)
  {
    if (!m.ContainsKey(str[i]))
    {
      m.Add(str[i], i);
    }
  }
 
  i = 0;
 
  // Store the last index of the
  // first character from map
  k = m[str[i]];
 
  for (i = 0; i < str.Length; i++)
  {
    if (i <= k)
    {
      c = c + 1;
 
      // Update K to find
      // the end of partition
      k = Math.Max(k, m[str[i]]);
    }
 
    // Otherwise, the end of
    // partition is found
    else
    {
      // Increment r
      r = r + 1;
      c = 1;
 
      // Update k for the
      // next partition
      k = Math.Max(k, m[str[i]]);
    }
  }
 
  // Add the last
  // partition
  if (c != 0)
  {
    r = r + 1;
  }
   
  return r;
}
 
// Driver code
public static void Main(String[] args)
{
  String str = "ababcbacadefegdehijhklij";
  Console.Write(maximum_partition(str));
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
3

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程