From 82103e5669de24307ca8f9db49470ceb04c6ad83 Mon Sep 17 00:00:00 2001 From: rrbharath Date: Tue, 28 Jul 2026 17:13:46 -0400 Subject: [PATCH] Add Dart solution for Valid Parentheses --- dart/valid_parentheses.dart | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 dart/valid_parentheses.dart diff --git a/dart/valid_parentheses.dart b/dart/valid_parentheses.dart new file mode 100644 index 0000000..ccf295d --- /dev/null +++ b/dart/valid_parentheses.dart @@ -0,0 +1,28 @@ +/* +Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. + +An input string is valid if open brackets are closed by the same type of brackets and in the correct order. +*/ + +class Solution { + bool isValid(String s) { + final Map pairs = { + ')': '(', + '}': '{', + ']': '[', + }; + final List stack = []; + + for (int i = 0; i < s.length; i++) { + final String char = s[i]; + + if (pairs.containsValue(char)) { + stack.add(char); + } else if (stack.isEmpty || stack.removeLast() != pairs[char]) { + return false; + } + } + + return stack.isEmpty; + } +}