Created with <3 with dartpad.dev.
Created
October 9, 2023 11:55
-
-
Save ervinod/236868f0c34b68166344943c47868165 to your computer and use it in GitHub Desktop.
problem-6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Check for Balanced Brackets in an expression (well-formedness) | |
| // Input: exp = “[()]{}{[()()]()}” Output: Balanced | |
| // Input: exp = “[(])” Output: Not Balanced | |
| void main() { | |
| final stringChecker = StringChecker1(); | |
| // final result = stringChecker.isValid("{([])}"); ///true | |
| // final result = stringChecker.isValid("()[]{}"); ///true | |
| final result = stringChecker.isValid("(]"); ///false | |
| print("String is well-formed: $result"); | |
| } | |
| class StringChecker1 { | |
| bool isValid(String expr) { | |
| if (expr.isEmpty) return true; | |
| List<String> stack = []; | |
| for (int i = 0; i < expr.length; i++) { | |
| final x = expr[i]; | |
| if (x == '(' || x == '[' || x == '{') { | |
| // add the element in the stack (array) | |
| stack.add(x); | |
| continue; | |
| } | |
| // If current character is not opening | |
| // bracket, then it must be closing. | |
| // So stack (array) cannot be empty at this point. | |
| if (stack.isEmpty) { | |
| return false; | |
| } | |
| final check; | |
| switch (x) { | |
| case ')': | |
| check = stack.removeLast(); | |
| if (check == '{' || check == '[') return false; | |
| break; | |
| case '}': | |
| check = stack.removeLast(); | |
| if (check == '(' || check == '[') return false; | |
| break; | |
| case ']': | |
| check = stack.removeLast(); | |
| if (check == '(' || check == '{') return false; | |
| break; | |
| } | |
| } | |
| // Check Empty Stack(array) | |
| return (stack.isEmpty); | |
| } | |
| } | |
| class StringChecker2 { | |
| bool isValid(String expr) { | |
| if (expr.isEmpty) return true; | |
| // map of parenthesis that we are checking basically | |
| Map<String, String> parenthesis = <String, String>{ | |
| ")": "(", | |
| "}": "{", | |
| "]": "[", | |
| }; | |
| // lis to save the values inside | |
| List<String> list = <String>[]; | |
| // for loop to check the every entered item in string | |
| for (String item in expr.split("")) { | |
| // checking if the entered string contain the values that inside are same | |
| if (parenthesis.containsKey(item)) { | |
| // if the it's zero and length is less than zero or negative | |
| // OR the it is not same aas the parenthesis than we will return false | |
| if (list.isEmpty || list[list.length - 1] != parenthesis[item]) { | |
| return false; | |
| } else { | |
| // removing the last values | |
| list.removeLast(); | |
| } | |
| } else { | |
| // than adding to the list | |
| list.add(item); | |
| } | |
| } | |
| // if the entered value is empty than it will be false if not than it | |
| // it will be true | |
| return list.isEmpty; | |
| } | |
| } | |
| class StringChecker3 { | |
| bool isValid(String expr) { | |
| if (expr.isEmpty) return true; | |
| List<String> stack = <String>[]; | |
| for (var i = 0; i < expr.length; i++) { | |
| if ('([{'.contains(expr[i])) { | |
| stack.add(expr[i]); | |
| } else { | |
| if (expr[i] == ')' && | |
| stack.isNotEmpty && | |
| stack[stack.length - 1] == '(') { | |
| stack.removeLast(); | |
| } else if (expr[i] == ']' && | |
| stack.isNotEmpty && | |
| stack[stack.length - 1] == '[') { | |
| stack.removeLast(); | |
| } else if (expr[i] == '}' && | |
| stack.isNotEmpty && | |
| stack[stack.length - 1] == '{') { | |
| stack.removeLast(); | |
| } else { | |
| return false; | |
| } | |
| } | |
| } | |
| return stack.isEmpty; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment