You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*`A`[Travelling Salesman Problem](src/algorithms/graph/travelling-salesman) - shortest possible route that visits each city and returns to the origin city
204
206
*`A`[Discrete Fourier Transform](src/algorithms/math/fourier-transform) - decompose a function of time (a signal) into the frequencies that make it up
@@ -229,7 +231,7 @@ algorithm is an abstraction higher than a computer program.
*`B`[Power Set](src/algorithms/sets/power-set) - all subsets of a set
@@ -254,8 +256,8 @@ different path of finding a solution. Normally the DFS traversal of state-space
254
256
*`A`[Combination Sum](src/algorithms/sets/combination-sum) - find all combinations that form specific sum
255
257
***Branch & Bound** - remember the lowest-cost solution found at each stage of the backtracking
256
258
search, and use the cost of the lowest-cost solution found so far as a lower bound on the cost of
257
-
a least-cost solution to the problem, in order to discard partial solutions with costs larger than the
258
-
lowest-cost solution found so far. Normally BFS traversal in combination with DFS traversal of state-space
259
+
a least-cost solution to the problem in order to discard partial solutions with costs larger than the
260
+
lowest-cost solution found so far. Normally, BFS traversal in combination with DFS traversal of state-space
259
261
tree is being used.
260
262
261
263
## How to use this repository
@@ -295,14 +297,14 @@ rm -rf ./node_modules
295
297
npm i
296
298
```
297
299
298
-
Also make sure that you're using a correct Node version (`>=16`). If you're using [nvm](https://github.com/nvm-sh/nvm) for Node version management you may run `nvm use` from the root folder of the project and the correct version will be picked up.
300
+
Also, make sure that you're using the correct Node version (`>=16`). If you're using [nvm](https://github.com/nvm-sh/nvm) for Node version management you may run `nvm use` from the root folder of the project and the correct version will be picked up.
299
301
300
302
**Playground**
301
303
302
304
You may play with data-structures and algorithms in `./src/playground/playground.js` file and write
303
305
tests for it in `./src/playground/__test__/playground.test.js`.
304
306
305
-
Then just simply run the following command to test if your playground code works as expected:
307
+
Then just, simply run the following command to test if your playground code works as expected:
306
308
307
309
```
308
310
npm test -- 'playground'
@@ -318,7 +320,7 @@ npm test -- 'playground'
318
320
### Big O Notation
319
321
320
322
*Big O notation* is used to classify algorithms according to how their running time or space requirements grow as the input size grows.
321
-
On the chart below you may find most common orders of growth of algorithms specified in Big O notation.
323
+
On the chart below, you may find the most common orders of growth of algorithms specified in Big O notation.
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
4
+
5
+
An input string is valid if:
6
+
7
+
Open brackets must be closed by the same type of brackets.
8
+
Open brackets must be closed in the correct order.
9
+
Every close bracket has a corresponding open bracket of the same type.
10
+
11
+
12
+
Example 1:
13
+
14
+
`Input: s = "()"`
15
+
16
+
Output: true
17
+
18
+
Example 2:
19
+
20
+
`Input: s = "()[]{}"`
21
+
22
+
Output: true
23
+
24
+
Example 3:
25
+
26
+
`Input: s = "(]"`
27
+
28
+
Output: false
29
+
30
+
This is actually a very common interview question and a very good example of how to use a stack data structure to solve problems.
31
+
32
+
## Solution
33
+
The problem can be solved in two ways
34
+
35
+
### Bruteforce Approach
36
+
We can iterate through the string and then for each character in the string, we check for it's last closing character in the the string. Once we find the last closing character in the string, we remove both characters and then repeat the iteration, if we don't find a closing character for an opening character, then the string is invalid. The time complexity of this would be O(n^2) which is not so efficient.
37
+
38
+
### Using a Stack
39
+
We can use a hashtable to store all opening characters and the value would be the respective closing character. We can then iterate through the string and if we encounter an opening parantheses, we push it's closing character to the stack. If we ecounter a closing paraentheses, then we pop the stack and confirm that the popped element is equal to the current closing parentheses character. If it is not then the string is invalid. At the end of the iteration, we also need to check that the stack is empty. If it is not then the string is invalid. If it is, then the string is valid. This is a more efficient approach with a Time complexity and Space complexity of O(n).
0 commit comments