File tree 1 file changed +14
-18
lines changed
1 file changed +14
-18
lines changed Original file line number Diff line number Diff line change @@ -6,36 +6,32 @@ var isValid = function (s) {
6
6
} ;
7
7
8
8
var chars = s . split ( "" ) ;
9
+ var isValidString = true ;
9
10
10
11
if ( chars . length % 2 !== 0 ) {
11
12
return false ;
12
13
}
13
14
14
- var isValid = true ;
15
- var listSize = chars . length ;
15
+ var index = 0 ;
16
16
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 ] ;
21
21
22
- var charFromReverse = chars [ s . length - ( i + 1 ) ] ;
23
-
24
- if ( nextChar == dictValue ) {
25
- i ++ ;
26
- continue ;
22
+ if ( ! nextChar ) {
23
+ isValidString = false ;
27
24
}
28
25
29
- if ( dictValue == charFromReverse ) {
30
- listSize -- ;
26
+ if ( nextChar == objValue ) {
27
+ chars . splice ( index , 2 ) ;
28
+ index = 0 ;
31
29
continue ;
32
30
}
33
31
34
- if ( ! ( nextChar == dictValue ) ) {
35
- isValid = false ;
36
- }
32
+ index ++ ;
37
33
}
38
- return isValid ;
34
+ return chars . length == 0 ? true : false ;
39
35
} ;
40
36
41
37
// 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) {
47
43
// "([])"
48
44
// "(([]){})"
49
45
50
- console . log ( isValid ( "()" ) ) ;
46
+ console . log ( isValid ( "(([]){} )" ) ) ;
You can’t perform that action at this time.
0 commit comments