📜  查找字符串中平衡位置的数量

📅  最后修改于: 2022-05-13 01:57:07.102000             🧑  作者: Mango

查找字符串中平衡位置的数量

给定一个字符串,任务是从该字符串的左右部分包含相同字符的字符串中找出这种平衡位置的数量。字符的频率无关紧要。

例子:

Input : str[] = abaaba
Output : Number of balancing positions : 3
Explanations : All 3 balancing positions are as :
ab|aaba, aba|aba, abaa|ba

Input : str[] = noon
Output : Number of balancing positions : 1
Explanations : Balancing position is :
no|on

朴素方法:如果我们试图用朴素的方法解决这个问题,我们必须处理字符串的所有 n 个位置,并且在每个位置,我们必须检查从该位置开始的字符串的左右部分是否具有相同的字符或不是。
查找位置是否平衡的过程(两个部分的频率不必相同)可以在 O(n^2) 时间内完成单个位置(我们应该检查左侧部分中的每个元素是否是出现在正确的位置,反之亦然)。这整个过程将导致一个时间复杂度为 O(n^3) 的算法。
高效方法:高效算法的想法来自这篇文章。主要区别在于我们不应该关心相等的频率,而是使用遍历字符串。
我们首先用所有字符的计数填充 right[]。然后我们从左到右遍历字符串。对于每个字符,我们在 left[] 中增加其计数并在 right[] 中减少计数。对于被遍历的任何点,如果左侧具有非零值的所有字符在右侧也具有非零值,反之亦然,那么我们将结果递增。

C++
// C++ program to find number of balancing
// points in string
#include
using namespace std;
const int MAX_CHAR = 256;
 
// function to return number of balancing points
int countBalance(char *str)
{
    int n = strlen(str); // string length
 
    // hash array for storing hash of string
    // initialized by 0 being global
    int leftVisited[MAX_CHAR] = {0};
    int rightVisited[MAX_CHAR] = {0};
 
    // process string initially for rightVisited
    for (int i=0; i


Java
// Java program to find number of balancing
// points in string
 
class GFG
{
    static final int MAX_CHAR = 256;
     
    // method to return number of balancing points
    static int countBalance(String s)
    {
        char[] str=s.toCharArray();
        int n = str.length; // string length
         
     
        // hash array for storing hash of string
        // initialized by 0 being global
        int[] rightVisited = new int[MAX_CHAR];
        int[] leftVisited = new int[MAX_CHAR];
     
        // process string initially for rightVisited
        for (int i=0; i


Python3
# Python3 program to find number of
# balancing points in string
MAX_CHAR = 256
 
# function to return number of
# balancing points
def countBalance(string):
 
    n = len(string) # string length
 
    # hash array for storing hash of
    # string initialized by 0 being global
    leftVisited = [0] * (MAX_CHAR)
    rightVisited = [0] * (MAX_CHAR)
 
    # process string initially for
    # rightVisited
    for i in range(0, n):
        rightVisited[ord(string[i])] += 1
 
    # check for balancing points
    res = 0
    for i in range(0, n):
     
        # for every position inc left
        # hash & dec rightVisited
        leftVisited[ord(string[i])] += 1
        rightVisited[ord(string[i])] -= 1
 
        # check whether both hash have
        # same character or not
        j = 0
        while j < MAX_CHAR:
         
            # Either both leftVisited[j] and
            # rightVisited[j] should have none
            # zero value or both should have
            # zero value
            if ((leftVisited[j] == 0 and
                 rightVisited[j] != 0) or
                (leftVisited[j] != 0 and
                 rightVisited[j] == 0)):
                break
             
            j += 1
 
        # if both have same character
        # increment count
        if j == MAX_CHAR:
            res += 1
     
    return res
 
# Driver Code
if __name__ == "__main__":
 
    string = "abaababa"
    print(countBalance(string))
     
# This code is contributed
# by Rituraj Jain


C#
// C# program to find number of
// balancing points in string
using System;
 
class GFG
{
    static int MAX_CHAR = 256;
     
    // method to return number of
    // balancing points
    static int countBalance(string s)
    {
        //char[] str=s.toCharArray();
        int n = s.Length; // string length
         
     
        // hash array for storing hash of string
        // initialized by 0 being global
        int[] rightVisited = new int[MAX_CHAR];
        int[] leftVisited = new int[MAX_CHAR];
     
        // process string initially for rightVisited
        for (int i = 0; i < n; i++)
            rightVisited[s[i]]++;
     
        // check for balancing points
        int res = 0;
        for (int i = 0; i < n; i++)
        {
            // for every position inc left
            // hash & dec rightVisited
            leftVisited[s[i]]++;
            rightVisited[s[i]]--;
     
            // check whether both hash have
            // same character or not
            int j;
            for (j = 0; j < MAX_CHAR; j++)
            {
                // Either both leftVisited[j] and
                // rightVisited[j] should have none
                // zero value or both should have
                // zero value
                if ((leftVisited[j] == 0 &&
                    rightVisited[j] != 0) ||
                    (leftVisited[j] != 0 &&
                    rightVisited[j] == 0))
                     
                    break;
            }
     
            // if both have same character
            // increment count
            if (j == MAX_CHAR)
                res++;
        }
        return res;
    }
     
    // Driver Code
    public static void Main(String []args)
    {
        string str = "abaababa";
        Console.WriteLine(countBalance(str));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

5