-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.rb
More file actions
53 lines (33 loc) · 802 Bytes
/
Solution.rb
File metadata and controls
53 lines (33 loc) · 802 Bytes
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
# @param {String} s
# @return {Boolean}
def is_valid(s)
if s == nil or s.size == 0 or s.size % 2 != 0
return false
end
strSize = s.size
stackSize = Integer(s.size / 2)
stack = Array.new stackSize
stackTop = -1
strIndex = 0
stackTop += 1
stack[stackTop] = s[strIndex]
strIndex += 1
while strIndex < strSize
if s[strIndex] == '(' or s[strIndex] == '[' or s[strIndex] == '{'
stackTop += 1
stack[stackTop] = s[strIndex]
else
if s[strIndex] == ')' and stack[stackTop] != '(' or s[strIndex] == ']' and stack[stackTop] != '[' or s[strIndex] == '}' and stack[stackTop] != '{'
return false
end
stackTop -= 1
end
strIndex += 1
end
if stackTop >= 0
false
else
true
end
end
puts is_valid '{}{}{}'