📌  相关文章
📜  最大字符串分区

📅  最后修改于: 2021-10-26 06:38:36             🧑  作者: Mango

给定一个字符串。任务是找到最大数P ,这样一个给定的字符串可以被分割成 P 个连续的子字符串,这样任意两个相邻的子字符串必须不同。更正式地说, S = S_{1}S_{2}....S_{P} S_{i} \ne S_{i + 1}(0 \leq i \leq P - 1) .

例子:

方法:

  • 这里我们只需要关注 P 的值,而不是找到那些 P 子串。
  • 我们会贪婪地解决它。我们总是检查我们拥有的当前字符串和已经使用的前一个字符串。
  • 如果发现两者相同,则继续前进,否则,在此处创建一个分区,将字符串的上一曲目更改为当前字符串,这意味着我们将此当前字符串视为前一个字符串未来比较。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Return the count of string
int maxPartition(string s)
{
    // P will store the answer
    int n = s.length(), P = 0;
 
    // Current will store current string
    // Previous will store the previous
    // string that has been taken already
    string current = "", previous = "";
 
    for (int i = 0; i < n; i++) {
 
        // Add a character to current string
        current += s[i];
 
        if (current != previous) {
 
            // Here we will create a partition and
            // update the previous string with
            // current string
            previous = current;
 
            // Now we will clear the current string
            current.clear();
 
            // Increment the count of partition.
            P++;
        }
    }
 
    return P;
}
 
// Driver code
int main()
{
 
    string s = "geeksforgeeks";
 
    int ans = maxPartition(s);
 
    cout << ans << "\n";
 
    return 0;
}


Java
// Java implementation of the above approach
class GFG
{
// Return the count of string
static int maxPartition(String s)
{
    // P will store the answer
    int n = s.length(), P = 0;
 
    // Current will store current string
    // Previous will store the previous
    // string that has been taken already
    String current = "", previous = "";
 
    for (int i = 0; i < n; i++)
    {
 
        // Add a character to current string
        current += s.charAt(i);
 
        if (!current.equals(previous))
        {
 
            // Here we will create a partition and
            // update the previous string with
            // current string
            previous = current;
 
            // Now we will clear the current string
            current = "";
 
            // Increment the count of partition.
            P++;
        }
    }
    return P;
}
 
// Driver code
public static void main (String[] args)
{
    String s = "geeksforgeeks";
 
    int ans = maxPartition(s);
 
    System.out.println(ans);
}
}
 
// This code is contributed by ihritik


Python3
# Python3 implementation of the above approach
 
# Return the count of string
def maxPartition(s):
     
    # P will store the answer
    n = len(s)
    P = 0
 
    # Current will store current string
    # Previous will store the previous
    # that has been taken already
    current = ""
    previous = ""
 
    for i in range(n):
 
        # Add a character to current string
        current += s[i]
 
        if (current != previous):
 
            # Here we will create a partition and
            # update the previous with
            # current string
            previous = current
 
            # Now we will clear the current string
            current = ""
 
            # Increment the count of partition.
            P += 1
 
    return P
 
# Driver code
s = "geeksforgeeks"
 
ans = maxPartition(s)
 
print(ans)
 
# This code is contributed by Mohit Kumar


C#
// C# implementation of the above approach
using System;
class GFG
{
// Return the count of string
static int maxPartition(string s)
{
    // P will store the answer
    int n = s.Length, P = 0;
 
    // Current will store current string
    // Previous will store the previous
    // string that has been taken already
    string current = "", previous = "";
 
    for (int i = 0; i < n; i++)
    {
 
        // Add a character to current string
        current += s[i];
 
        if (!current.Equals(previous))
        {
 
            // Here we will create a partition and
            // update the previous string with
            // current string
            previous = current;
 
            // Now we will clear the current string
            current = "";
 
            // Increment the count of partition.
            P++;
        }
    }
    return P;
}
 
// Driver code
public static void Main ()
{
    string s = "geeksforgeeks";
 
    int ans = maxPartition(s);
 
    Console.WriteLine(ans);
}
}
 
// This code is contributed by ihritik


Javascript


输出:
11

时间复杂度: O(N),其中 N 是字符串的长度。

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