4. Design Approach
02
• This approach uses recursion and backtracking to find all possible
paranthesizations of a given string of symbols from a given set of
characters.
• The function isSymbolPossible() tries to find all possible combinations of
paranthesizations by calling itself on substrings of the input string, and using
the multiplication table to check if the symbol of interest can be generated
by the current combination of substrings.
• The function returns the paranthesization of the string if it is possible and an
empty string if it is not. Time complexity of this approach is O(n^4) which is
not efficient for large inputs.
5. static char[ ] alphabet = { 'a', 'b', 'c' };
static int k = 3;
static char[ ][ ] multiplicationTable = new char[k][k];
static String paranthesization = "";
Defined Variables:-
Code
03
6. Main Funtion:-
public static void main(String[ ] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the String:(containing 'a' 'b'
'c')");
String input = in.nextLine();
int N = input.length();
multiplicationTable [0][0] = 'c';
multiplicationTable [0][1] = 'c';
multiplicationTable [0][2] = 'b';
multiplicationTable [1][0] = 'a';
multiplicationTable [1][1] = 'c';
multiplicationTable [1][2] = 'b';
multiplicationTable [2][0] = 'b';
multiplicationTable [2][1] = 'a';
multiplicationTable [2][2] = 'a';
if (isSymbolPossible(s, 'a', N) != "") {
System.out.println("Yes");
System.out.println("parentisation:" + paranthesization);
System.out.println("[" + s + "]");
} else {
System.out.println("No");
}
}
}
7. isSymbolPossible Funtion :-
static String isSymbolPossible(String s, char symbol, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j1 = 0; j1 < k; j1++) {
for (int j2 = 0; j2 < k; j2++) {
if (multiplicationTable[j1][j2] == symbol) {
String left = isSymbolPossible(s.substring(0, i + 1), alphabet[j1], i + 1);
String right = isSymbolPossible(s.substring(i + 1, n), alphabet[j2], n - i -
1);
if (!left.isEmpty() && !right.isEmpty()) {
paranthesization = "(" + left + right + ")";
return paranthesization;
}
}
}
}
}
if (n == 1 && s.charAt(0) == symbol) {
return s;
}
return "";
}
8. Analysis
04
The time complexity of this algorithm is O(k^n), where k is the number of
possible characters in the alphabet and n is the length of the input string. This is
because the algorithm iterates through all possible split positions, symbols, and
substrings of the input string, and for each possible combination, it recursively
calls itself. The number of recursive calls is equal to the number of substrings of
the input string, which is O(n^2), and for each call, it iterates through all possible
symbols, which is O(k).
The space complexity of this algorithm is O(n^2) because it uses recursion and
the maximum depth of the recursion is equal to the length of the input string.
Each recursive call consumes O(1) space, but the maximum number of recursive
calls is equal to the number of substrings of the input string, which is O(n^2).
10. Conclusion
05
The above code is a recursive algorithm that attempts to find a way to
parenthesize the input string s such that the resulting mathematical expression
equals the desired symbol 'a'. It does this by iterating through all possible split
positions in the string, and for each split position, all possible symbols that could
be present in the left and right substrings resulting from the split. It then
recursively calls itself on the left and right substrings, passing in the appropriate
symbol as the second parameter.
The algorithm then checks if both the left and right substrings have a
parenthesization that results in the desired symbol, concatenates the
parenthesization of the left and right substrings with appropriate parentheses and
assigns it to the paranthesization variable.