Given a string, the task is to find the number of such balancing positions in the string from where the left and the right part of that string contains the same characters. The frequency of characters doesn’t matter.
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
Naive Approach : If we try to solve this problem by the naive approach, we have to process for all n positions of string and at each position, we must check whether the left and right parts of our string from that position have same characters or not.
The process of finding whether the position is balancing or not (frequency of both parts need not be the same) can be done in O(n^2) time for a single position( where we should check if each element in the left part is present in right and vice-versa). This whole process will lead to an algorithm of time complexity O(n^3).
Efficient Approach: Idea of efficient algorithm came from this article. The main difference is that we should not care about equal frequency, and using traversing the string.
We first fill right with counts of all characters. Then we traverse the string from left to right. For every character, we increment its count in left and decrement count in right. For any point being traversed, if all characters that have a non-zero value in left also have a non-zero value in right, and vice versa is also true, then we increment result.
This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Return maximum occurring character in an input string
- Print all the duplicates in the input string
- Remove characters from the first string which are present in the second string
- Remove duplicates from a given string
- Print reverse of a string using recursion
- Write a program to print all permutations of a given string
- Divide a string in N equal parts
- Given a string, find its first non-repeating character
- Write a program to reverse an array or string
- Reverse words in a given string
- Find the smallest window in a string containing all characters of another string
- Given a number, find the next smallest palindrome
- Lexicographic rank of a string
- An in-place algorithm for String Transformation
- Count words in a given string
- String matching where one string contains wildcard characters
- Remove "b" and "ac" from a given string
- Find if a string is interleaved of two other strings | DP-33
- Find the first non-repeating character from a stream of characters
- Print all ways to break a string in bracket form