SlideShare a Scribd company logo
1 of 11
Parenthesization
CONTENTS
01
Problem
statement
Explaining of
the problem
02
Design Approach
Implementation
method
03
Code
Java code
for the
same
05
Conclusion
Conclusion
04
Analysis
Analysing
the code.
Problem Statement
01
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.
static char[ ] alphabet = { 'a', 'b', 'c' };
static int k = 3;
static char[ ][ ] multiplicationTable = new char[k][k];
static String paranthesization = "";
Defined Variables:-
Code
03
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");
}
}
}
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 "";
}
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).
Implementation In Online GDB:-Link
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.
THANK YOU

More Related Content

Similar to Parenthesization.pptx

Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiSowmya Jyothi
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++NUST Stuff
 
The Art of Java Type Patterns
The Art of Java Type PatternsThe Art of Java Type Patterns
The Art of Java Type PatternsSimon Ritter
 
regular-expression.pdf
regular-expression.pdfregular-expression.pdf
regular-expression.pdfDarellMuchoko
 
IT8761-SECURITY LABORATORY-590519304-IT8761 security labmanual.pdf
IT8761-SECURITY LABORATORY-590519304-IT8761 security labmanual.pdfIT8761-SECURITY LABORATORY-590519304-IT8761 security labmanual.pdf
IT8761-SECURITY LABORATORY-590519304-IT8761 security labmanual.pdfDhanuskarSankar1
 
Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosluzenith_g
 
Analysis of algorithms
Analysis of algorithms Analysis of algorithms
Analysis of algorithms MUSAIDRIS15
 
Linear Data Structures_SSD.pdf
Linear Data Structures_SSD.pdfLinear Data Structures_SSD.pdf
Linear Data Structures_SSD.pdfssuser37b0e0
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
Java scanner, everything you need to know about Java Scanner
Java scanner, everything you need to know about Java ScannerJava scanner, everything you need to know about Java Scanner
Java scanner, everything you need to know about Java ScannerEdward Nyang'ali
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumarSujith Kumar
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoreambikavenkatesh2
 

Similar to Parenthesization.pptx (20)

String.pdf
String.pdfString.pdf
String.pdf
 
Strings
StringsStrings
Strings
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
 
Team 1
Team 1Team 1
Team 1
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
 
The Art of Java Type Patterns
The Art of Java Type PatternsThe Art of Java Type Patterns
The Art of Java Type Patterns
 
regular-expression.pdf
regular-expression.pdfregular-expression.pdf
regular-expression.pdf
 
Ch2
Ch2Ch2
Ch2
 
Python data handling
Python data handlingPython data handling
Python data handling
 
IT8761-SECURITY LABORATORY-590519304-IT8761 security labmanual.pdf
IT8761-SECURITY LABORATORY-590519304-IT8761 security labmanual.pdfIT8761-SECURITY LABORATORY-590519304-IT8761 security labmanual.pdf
IT8761-SECURITY LABORATORY-590519304-IT8761 security labmanual.pdf
 
Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmos
 
Analysis of algorithms
Analysis of algorithms Analysis of algorithms
Analysis of algorithms
 
Alg1
Alg1Alg1
Alg1
 
String functions
String functionsString functions
String functions
 
Linear Data Structures_SSD.pdf
Linear Data Structures_SSD.pdfLinear Data Structures_SSD.pdf
Linear Data Structures_SSD.pdf
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
 
Java scanner, everything you need to know about Java Scanner
Java scanner, everything you need to know about Java ScannerJava scanner, everything you need to know about Java Scanner
Java scanner, everything you need to know about Java Scanner
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumar
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysore
 

Recently uploaded

Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxNANDHAKUMARA10
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxMustafa Ahmed
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxMustafa Ahmed
 
Path loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelPath loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelDrAjayKumarYadav4
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...archanaece3
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiessarkmank1
 
Lect.1: Getting Started (CS771: Machine Learning by Prof. Purushottam Kar, II...
Lect.1: Getting Started (CS771: Machine Learning by Prof. Purushottam Kar, II...Lect.1: Getting Started (CS771: Machine Learning by Prof. Purushottam Kar, II...
Lect.1: Getting Started (CS771: Machine Learning by Prof. Purushottam Kar, II...singhalabhi53
 
Databricks Generative AI Fundamentals .pdf
Databricks Generative AI Fundamentals  .pdfDatabricks Generative AI Fundamentals  .pdf
Databricks Generative AI Fundamentals .pdfVinayVadlagattu
 
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...Payal Garg #K09
 
Autodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptxAutodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptxMustafa Ahmed
 
Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)ChandrakantDivate1
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...josephjonse
 
DFT - Discrete Fourier Transform and its Properties
DFT - Discrete Fourier Transform and its PropertiesDFT - Discrete Fourier Transform and its Properties
DFT - Discrete Fourier Transform and its PropertiesShiny Christobel
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementDr. Deepak Mudgal
 
Passive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptPassive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptamrabdallah9
 
Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2ChandrakantDivate1
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 
Overview of Transformation in Computer Graphics
Overview of Transformation in Computer GraphicsOverview of Transformation in Computer Graphics
Overview of Transformation in Computer GraphicsChandrakantDivate1
 
Introduction to Geographic Information Systems
Introduction to Geographic Information SystemsIntroduction to Geographic Information Systems
Introduction to Geographic Information SystemsAnge Felix NSANZIYERA
 

Recently uploaded (20)

Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptx
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptx
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
Path loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelPath loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata Model
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
Lect.1: Getting Started (CS771: Machine Learning by Prof. Purushottam Kar, II...
Lect.1: Getting Started (CS771: Machine Learning by Prof. Purushottam Kar, II...Lect.1: Getting Started (CS771: Machine Learning by Prof. Purushottam Kar, II...
Lect.1: Getting Started (CS771: Machine Learning by Prof. Purushottam Kar, II...
 
Databricks Generative AI Fundamentals .pdf
Databricks Generative AI Fundamentals  .pdfDatabricks Generative AI Fundamentals  .pdf
Databricks Generative AI Fundamentals .pdf
 
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
 
Autodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptxAutodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptx
 
Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
DFT - Discrete Fourier Transform and its Properties
DFT - Discrete Fourier Transform and its PropertiesDFT - Discrete Fourier Transform and its Properties
DFT - Discrete Fourier Transform and its Properties
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth Reinforcement
 
Passive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptPassive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.ppt
 
Signal Processing and Linear System Analysis
Signal Processing and Linear System AnalysisSignal Processing and Linear System Analysis
Signal Processing and Linear System Analysis
 
Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Overview of Transformation in Computer Graphics
Overview of Transformation in Computer GraphicsOverview of Transformation in Computer Graphics
Overview of Transformation in Computer Graphics
 
Introduction to Geographic Information Systems
Introduction to Geographic Information SystemsIntroduction to Geographic Information Systems
Introduction to Geographic Information Systems
 

Parenthesization.pptx

  • 2. CONTENTS 01 Problem statement Explaining of the problem 02 Design Approach Implementation method 03 Code Java code for the same 05 Conclusion Conclusion 04 Analysis Analysing the code.
  • 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.