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 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(); + } +} diff --git a/ValidParentheses.md b/ValidParentheses.md new file mode 100644 index 00000000..4d990ad4 --- /dev/null +++ b/ValidParentheses.md @@ -0,0 +1,219 @@ +# 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= input() +print(isValidParanthesis(input)) +``` +## Code in java +``` +// 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(); + } +} + +``` +## 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;ibool: + + 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))