From 4689e4e80b479be25f7557d05818f5caa723aafa Mon Sep 17 00:00:00 2001
From: 3gg <3gg@shellblade.net>
Date: Wed, 5 Feb 2025 18:36:31 -0800
Subject: Initial commit.

---
 .../easy/others/05_valid_parentheses.cc            | 41 ++++++++++++++++++++++
 .../easy/others/06_missing_number.cc               | 13 +++++++
 2 files changed, 54 insertions(+)
 create mode 100644 top-interview-questions/easy/others/05_valid_parentheses.cc
 create mode 100644 top-interview-questions/easy/others/06_missing_number.cc

(limited to 'top-interview-questions/easy/others')

diff --git a/top-interview-questions/easy/others/05_valid_parentheses.cc b/top-interview-questions/easy/others/05_valid_parentheses.cc
new file mode 100644
index 0000000..3f6d020
--- /dev/null
+++ b/top-interview-questions/easy/others/05_valid_parentheses.cc
@@ -0,0 +1,41 @@
+class Solution {
+public:
+    char matchingChar(char c) {
+        switch (c) {
+            case ')': return '(';
+            case '}': return '{';
+            case ']': return '[';
+            default: throw;
+        }
+    }
+
+    bool isValid(string s) {
+        // ([])  -- valid
+        // ([)]  -- invalid
+        std::stack<char> st;
+        for (char c : s) {
+            switch (c) {
+                case '(':
+                case '{':
+                case '[': {
+                    st.push(c);
+                    break;
+                }
+                case ')':
+                case '}':
+                case ']': {
+                    const char expected = matchingChar(c);
+                    if (st.empty() ||
+                        (st.top() != expected)) {
+                        return false;
+                    }
+                    st.pop();
+                    break;
+                }
+                default:
+                    throw;
+            }
+        }
+        return st.empty();
+    }
+};
diff --git a/top-interview-questions/easy/others/06_missing_number.cc b/top-interview-questions/easy/others/06_missing_number.cc
new file mode 100644
index 0000000..0487bcc
--- /dev/null
+++ b/top-interview-questions/easy/others/06_missing_number.cc
@@ -0,0 +1,13 @@
+class Solution {
+public:
+    int missingNumber(vector<int>& nums) {
+        const size_t expectedSum = nums.size() * (nums.size()+1) / 2;
+
+        size_t sum = 0;
+        for (int x : nums) {
+            sum += x;
+        }
+
+        return expectedSum - sum;
+    }
+};
-- 
cgit v1.2.3