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 same characters. The frequency of characters doesn’t matters.
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 same) can be done in O(n^2) time for a single position( where we should check if each element in left part is present in right and vice-versa). This whole process will lead 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 non-zero value in left also have 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.
- Sort an alphanumeric string such that the positions of alphabets and numbers remain unchanged
- Find time taken for signal to reach all positions in a string
- Reverse a string preserving space positions
- Number of positions where a letter can be inserted such that a string becomes palindrome
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Find the missing number in a string of numbers with no separator
- Find the sum of the ascii values of characters which are present at prime positions
- Minimum Swaps for Bracket Balancing
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find the character in first string that is present at minimum index in second string
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Find length of longest subsequence of one string which is substring of another string
- Find the count of palindromic sub-string of a string in its sorted form
- Find the smallest window in a string containing all characters of another string
- Queries to find the first non-repeating character in the sub-string of a string