-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem.js
90 lines (79 loc) · 1.94 KB
/
problem.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
export class Stack {
constructor() {
//a simple array is used to implement a stack (LIFO model).
this.stackItems = [];
}
/*
Adds a new element to the stack (in the front position).
e.g.
existing stack:
10 (front)
9
6 (rear)
adding 5 as a new element would be:
5
10
9
6
*/
push(item) {
return this.stackItems.push(item);
}
/*
Removes the element from the stack (in the front position).
e.g.
existing stack:
5 (front)
10
9
6 (rear)
removing one element:
10 (front)
9
6 (rear)
*/
pop() {
if (this.stackItems.length > 0)
this.stackItems.pop();
}
/*
Returns the front element from the stack, but does not delete it.
e.g.
existing stack:
5 (front)
10
9
6 (rear)
returns 5.
*/
peek() {
if (this.stackItems.length > 0)
return this.stackItems[this.stackItems.length - 1];
return "No items in stack to return";
}
//Returns bool value - whether the stack is empty.
isEmpty() {
return this.stackItems.length === 0;
}
//Returns the length of the stack.
length() {
return this.stackItems.length;
}
//Returns the stack items (in the correct order) - mainly used for testing.
get() {
return this.stackItems;
}
}
/*
Check whether a certain string has matching pairs of delimiters.
Provide the time / space complexity for the algorithm implemented.
Note: The string will only be formed off the following accepted delimiters: "(", ")", "[", "]", "{", "}", """, "'".
e.g.
- Example string 1 : []()""{"[()]"}
Expected Result: Valid!
- Example string 2: []()""{"[()]]"}
Expected Result: Invalid! (13th character is not correct)
Note:
- Consider edge cases as well (e.g. empty stack, etc.)
- Write tests for the happy path, and for at least one unhappy path.
*/