Skip to content

Commit 8a1969b

Browse files
author
Kalgros20
committed
fixes valid parenthesis problem
1 parent a3bd3a9 commit 8a1969b

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

valid-parenthesis.js

+14-18
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,32 @@ var isValid = function (s) {
66
};
77

88
var chars = s.split("");
9+
var isValidString = true;
910

1011
if (chars.length % 2 !== 0) {
1112
return false;
1213
}
1314

14-
var isValid = true;
15-
var listSize = chars.length;
15+
var index = 0;
1616

17-
for (let i = 0; i < listSize; i++) {
18-
var char = chars[i];
19-
var nextChar = chars[i + 1];
20-
var dictValue = validStrings[char];
17+
while (isValidString) {
18+
var char = chars[index];
19+
var nextChar = chars[index + 1];
20+
var objValue = validStrings[char];
2121

22-
var charFromReverse = chars[s.length - (i + 1)];
23-
24-
if (nextChar == dictValue) {
25-
i++;
26-
continue;
22+
if (!nextChar) {
23+
isValidString = false;
2724
}
2825

29-
if (dictValue == charFromReverse) {
30-
listSize--;
26+
if (nextChar == objValue) {
27+
chars.splice(index, 2);
28+
index = 0;
3129
continue;
3230
}
3331

34-
if (!(nextChar == dictValue)) {
35-
isValid = false;
36-
}
32+
index++;
3733
}
38-
return isValid;
34+
return chars.length == 0 ? true : false;
3935
};
4036

4137
// Ou o match é o próximo caracter ou é o mesmo indice de trás pra frente do array.
@@ -47,4 +43,4 @@ var isValid = function (s) {
4743
// "([])"
4844
// "(([]){})"
4945

50-
console.log(isValid("()"));
46+
console.log(isValid("(([]){})"));

0 commit comments

Comments
 (0)