Given a string, find all ways to break the given string in bracket form. Enclose each substring within a parenthesis.
Input : abc Output: (a)(b)(c) (a)(bc) (ab)(c) (abc) Input : abcd Output : (a)(b)(c)(d) (a)(b)(cd) (a)(bc)(d) (a)(bcd) (ab)(c)(d) (ab)(cd) (abc)(d) (abcd)
We strongly recommend you to minimize your browser and try this yourself first.
The idea is to use recursion. We maintain two parameters – index of the next character to be processed and the output string so far. We start from index of next character to be processed, append substring formed by unprocessed string to the output string and recurse on remaining string until we process the whole string. We use std::substr to form the output string. substr(pos, n) returns a substring of length n that starts at position pos of current string.
Below diagram shows recursion tree for input string “abc”. Each node on the diagram shows processed string (marked by green) and unprocessed string (marked by red).
Below is its implementation-
(a)(b)(c)(d) (a)(b)(cd) (a)(bc)(d) (a)(bcd) (ab)(c)(d) (ab)(cd) (abc)(d) (abcd)
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. 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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print Bracket Number
- Find index of closing bracket for a given opening bracket in an expression
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket
- Construct Binary Tree from String with bracket representation
- Check if permutaion of one string can break permutation of another
- Minimal moves to form a string by adding characters or appending string itself
- Find the count of palindromic sub-string of a string in its sorted form
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Check if a string is a scrambled form of another string
- Print shortest path to print a string on screen
- Print given sentence into its equivalent ASCII form
- Print the longest prefix of the given string which is also the suffix of the same string
- Print all possible combinations of the string by replacing '$' with any other digit from the string
- Reverse string in Python (5 different ways)
- Find all possible ways to Split the given string into Primes
- Number of ways to convert a character X to a string Y
- Ways of transforming one string to other by removing 0 or more characters
- Ways to remove one element from a binary string so that XOR becomes zero
- Print string after removing all (“10” or “01”) from the binary string
- Number of ways to remove a sub-string from S such that all remaining characters are same