Given a string str of length N, the task is to find the number of ways to insert only 2 pairs of parentheses into the given string such that the resultant string is still valid.
Input: str = “ab”
((a))b, ((a)b), ((ab)), (a)(b), (a(b)), a((b))
which are a total of 6 ways.
Input: str = “aab”
Approach: it can be observed that for the lengths of the string 1, 2, 3, …, N a series will be formed as 1, 6, 20, 50, 105, 196, 336, 540, … whose Nth term is (N + 1)2 * ((N + 1)2 – 1) / 12.
Below is the implementation of the above approach:
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Count pairs of parentheses sequences such that parentheses are balanced
- Number of ways to insert a character to increase the LCS by one
- Ways of transforming one string to other by removing 0 or more characters
- Count pairs of characters in a string whose ASCII value difference is K
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Pairs involved in Balanced Parentheses
- Ways to form n/2 pairs such that difference of pairs is minimum
- Number of ways in which the substring in range [L, R] can be formed using characters out of the range
- Number of ways to convert a character X to a string Y
- Number of ways to partition a string into two balanced subsequences
- String with maximum number of unique characters
- Maximum number of characters between any two same character in a string
- Minimize number of unique characters in string
- Recursive program to insert a star between pair of identical characters
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.