From c70f81296823a3ff46f4242f812a56e91b08daeb Mon Sep 17 00:00:00 2001 From: Ajashwanth <137981688+Ajashwanth@users.noreply.github.com> Date: Mon, 11 Dec 2023 22:57:49 +0530 Subject: [PATCH 1/5] ValidParenthese --- ValidParentheses.md | 170 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 ValidParentheses.md diff --git a/ValidParentheses.md b/ValidParentheses.md new file mode 100644 index 00000000..2abe1f50 --- /dev/null +++ b/ValidParentheses.md @@ -0,0 +1,170 @@ +# ValidParentheses +The problem involves checking if a given string containing various types of parentheses (such as parentheses "()", square brackets "[]", and curly braces "{}") is valid, meaning that the parentheses are properly nested and closed. +## Introduction + + #### Here are the basic rules for a string with valid parentheses: + +Each opening parenthesis must have a corresponding closing parenthesis of the same type. +The parentheses must be properly nested; for example, "([])" is valid, but "([)]" is not. +This problem is often used to test a programmer's understanding of stack data structures and their ability to handle nested structures. The solution typically involves using a stack to keep track of the opening parentheses encountered, and then checking if the corresponding closing parentheses match when encountered. + + ## Overview +ValidParentheses is a program that checks if a given string of parentheses is valid or not. It uses a stack data structure to keep track of the opening and closing parentheses. The program returns true if the parentheses are balanced and false otherwise. It is commonly used in programming interviews and coding challenges to test a candidate's understanding of stack operations and string manipulation. + +## Code in python +``` + # Copyrights to venkys.io +# For more information, visit https://venkys.io +def isValidParanthesis(string:str)->bool: + + stack=list() + + for char in string: + if char=="(" or char=="{" or char=="[": + stack.append(char) + else: + if not stack: + return False + top=stack.pop() + if char==")" and top!="(": + return False + if char=="}" and top!="{": + return False + if char=="]" and top!="[": + return False + return len(stack)==0 + +input= "()[]{}" +print(isValidParanthesis(input)) +``` +## Code in java +``` +// Copyrights to venkys.io +// For more information, visit https://venkys.io +public boolean isValid(String s) { + Stack stack = new Stack(); + for (char c : s.toCharArray()) { + if (c == '(') + stack.push(')'); + else if (c == '{') + stack.push('}'); + else if (c == '[') + stack.push(']'); + else if (stack.isEmpty() || stack.pop() != c) + return false; + } + return stack.isEmpty(); +} +``` +## Code in cpp +``` +/* Copyrights to venkys.io*/ +/* For more information, visit https://venkys.io */ + +#include +using namespace std; + + +bool VSD_Valid_Parentheses(string str){ + int len=str.length(); + if(str.size()%2 !=0){ + return false; + } + else{ + stackstack_VSD; + unordered_map VSD={{'(',')'},{'[',']'},{'{','}'}}; + for(int i=0;i Date: Tue, 26 Mar 2024 00:58:38 +0530 Subject: [PATCH 2/5] Update ValidParentheses.md --- ValidParentheses.md | 85 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 67 insertions(+), 18 deletions(-) diff --git a/ValidParentheses.md b/ValidParentheses.md index 2abe1f50..4d990ad4 100644 --- a/ValidParentheses.md +++ b/ValidParentheses.md @@ -34,27 +34,51 @@ def isValidParanthesis(string:str)->bool: return False return len(stack)==0 -input= "()[]{}" +input= input() print(isValidParanthesis(input)) ``` ## Code in java ``` // Copyrights to venkys.io // For more information, visit https://venkys.io -public boolean isValid(String s) { - Stack stack = new Stack(); - for (char c : s.toCharArray()) { - if (c == '(') - stack.push(')'); - else if (c == '{') - stack.push('}'); - else if (c == '[') - stack.push(']'); - else if (stack.isEmpty() || stack.pop() != c) - return false; - } - return stack.isEmpty(); +import java.util.Scanner; +import java.util.Stack; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + // Read the string input from the user + String inputString = scanner.nextLine(); + + // Check if the input string is valid using the isValid function + boolean isValid = isValid(inputString); + + // Display the result + if (isValid) + System.out.println("True"); + else + System.out.println("False"); + + scanner.close(); + } + + public static boolean isValid(String s) { + Stack stack = new Stack(); + for (char c : s.toCharArray()) { + if (c == '(') + stack.push(')'); + else if (c == '{') + stack.push('}'); + else if (c == '[') + stack.push(']'); + else if (stack.isEmpty() || stack.pop() != c) + return false; + } + return stack.isEmpty(); + } } + ``` ## Code in cpp ``` @@ -105,9 +129,11 @@ bool VSD_Valid_Parentheses(string str){ } int main() { - string str="()[]{}"; - if(VSD_Valid_Parentheses(str)) cout<<"It is valid Parantheses"; - else cout<<"It is not valid Parantheses"; + // Read the string input from the user + std::string str; + std::getline(std::cin, str); + if(VSD_Valid_Parentheses(str)) cout<<"True"; + else cout<<"False"; return 0; } @@ -167,4 +193,27 @@ When writing code, the IDE or text editor often checks for syntax errors, includ In web development and XML processing, nested structures are common. Validating the correctness of tags and their nesting is crucial for ensuring well-formed documents. - Mathematical Expressions Evaluation: -In applications involving mathematical expressions, parentheses are used to indicate the order of operations. Validating the correctness of parentheses ensures that the expression is evaluated correctly. \ No newline at end of file +In applications involving mathematical expressions, parentheses are used to indicate the order of operations. Validating the correctness of parentheses ensures that the expression is evaluated correctly. + +## Test Cases + +- Input: "()[]{}" + Output: True + + Explanation: + The input string contains valid pairs of parentheses, square brackets, and curly braces. + Therefore, the output should be True. + +- Input: "([)]" + Output: False + + Explanation: + The input string contains an invalid pair of parentheses and square brackets. + Therefore, the output should be False. + +- Input: "{[]}" + Output: True + + Explanation: + The input string contains valid pairs of parentheses, square brackets, and curly braces. + Therefore, the output should be True. From e6cfdd8725b64a6e54ffc90177878f167b8fea92 Mon Sep 17 00:00:00 2001 From: Sneha Vellelath <129183233+vellsneha@users.noreply.github.com> Date: Tue, 26 Mar 2024 01:02:03 +0530 Subject: [PATCH 3/5] Create ValidParentheses.py --- ValidParentheses.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 ValidParentheses.py diff --git a/ValidParentheses.py b/ValidParentheses.py new file mode 100644 index 00000000..cb17aa29 --- /dev/null +++ b/ValidParentheses.py @@ -0,0 +1,23 @@ +# Copyrights to venkys.io +# For more information, visit https://venkys.io +def isValidParanthesis(string:str)->bool: + + stack=list() + + for char in string: + if char=="(" or char=="{" or char=="[": + stack.append(char) + else: + if not stack: + return False + top=stack.pop() + if char==")" and top!="(": + return False + if char=="}" and top!="{": + return False + if char=="]" and top!="[": + return False + return len(stack)==0 + +input= input() +print(isValidParanthesis(input)) From 4b95ea11dfc6057b2377ee56bbe5657b25d0c56d Mon Sep 17 00:00:00 2001 From: Sneha Vellelath <129183233+vellsneha@users.noreply.github.com> Date: Tue, 26 Mar 2024 01:02:51 +0530 Subject: [PATCH 4/5] Create ValidParentheses.java --- ValidParentheses.java | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 ValidParentheses.java diff --git a/ValidParentheses.java b/ValidParentheses.java new file mode 100644 index 00000000..b904bdaf --- /dev/null +++ b/ValidParentheses.java @@ -0,0 +1,39 @@ +// Copyrights to venkys.io +// For more information, visit https://venkys.io +import java.util.Scanner; +import java.util.Stack; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + // Read the string input from the user + String inputString = scanner.nextLine(); + + // Check if the input string is valid using the isValid function + boolean isValid = isValid(inputString); + + // Display the result + if (isValid) + System.out.println("True"); + else + System.out.println("False"); + + scanner.close(); + } + + public static boolean isValid(String s) { + Stack stack = new Stack(); + for (char c : s.toCharArray()) { + if (c == '(') + stack.push(')'); + else if (c == '{') + stack.push('}'); + else if (c == '[') + stack.push(']'); + else if (stack.isEmpty() || stack.pop() != c) + return false; + } + return stack.isEmpty(); + } +} From e19be6f51bc4c52bc259566bf8d218ab3cc5658d Mon Sep 17 00:00:00 2001 From: Sneha Vellelath <129183233+vellsneha@users.noreply.github.com> Date: Tue, 26 Mar 2024 01:03:41 +0530 Subject: [PATCH 5/5] Create ValidParentheses.cpp --- ValidParentheses.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 ValidParentheses.cpp diff --git a/ValidParentheses.cpp b/ValidParentheses.cpp new file mode 100644 index 00000000..f5a0c260 --- /dev/null +++ b/ValidParentheses.cpp @@ -0,0 +1,55 @@ +/* Copyrights to venkys.io*/ +/* For more information, visit https://venkys.io */ + +#include +using namespace std; + + +bool VSD_Valid_Parentheses(string str){ + int len=str.length(); + if(str.size()%2 !=0){ + return false; + } + else{ + stackstack_VSD; + unordered_map VSD={{'(',')'},{'[',']'},{'{','}'}}; + for(int i=0;i