File tree 2 files changed +87
-0
lines changed
2 files changed +87
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string[] } strs
3
+ * @return {string }
4
+ */
5
+ var longestCommonPrefix = function ( strs ) {
6
+ var hasPrefix = true ;
7
+ var commonIndex = 0 ;
8
+ var commonPrefix = "" ;
9
+ var index = 0 ;
10
+
11
+ while ( hasPrefix ) {
12
+ hasPrefix = strs [ index ] . startsWith ( commonPrefix ) ;
13
+
14
+ if ( index === strs . length - 1 ) {
15
+ if ( hasPrefix ) {
16
+ commonPrefix = commonPrefix + ( strs [ 0 ] [ commonIndex ] || "" ) ;
17
+
18
+ if ( commonPrefix == "" ) {
19
+ return "" ;
20
+ }
21
+
22
+ if ( commonIndex === strs [ 0 ] . length ) {
23
+ return commonPrefix ;
24
+ }
25
+
26
+ // Increment common index to check next character
27
+ commonIndex ++ ;
28
+ // Reset index to initial point of list
29
+ index = 0 ;
30
+ }
31
+ } else {
32
+ index ++ ;
33
+ }
34
+ }
35
+
36
+ return commonPrefix . slice ( 0 , - 1 ) ;
37
+ } ;
Original file line number Diff line number Diff line change
1
+ var isValid = function ( s ) {
2
+ const validStrings = {
3
+ "(" : ")" ,
4
+ "{" : "}" ,
5
+ "[" : "]" ,
6
+ } ;
7
+
8
+ var chars = s . split ( "" ) ;
9
+
10
+ if ( chars . length % 2 !== 0 ) {
11
+ return false ;
12
+ }
13
+
14
+ var isValid = true ;
15
+ var listSize = chars . length ;
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 ] ;
21
+
22
+ var charFromReverse = chars [ s . length - ( i + 1 ) ] ;
23
+
24
+ if ( nextChar == dictValue ) {
25
+ i ++ ;
26
+ continue ;
27
+ }
28
+
29
+ if ( dictValue == charFromReverse ) {
30
+ listSize -- ;
31
+ continue ;
32
+ }
33
+
34
+ if ( ! ( nextChar == dictValue ) ) {
35
+ isValid = false ;
36
+ }
37
+ }
38
+ return isValid ;
39
+ } ;
40
+
41
+ // Ou o match é o próximo caracter ou é o mesmo indice de trás pra frente do array.
42
+ // senão tiver match,
43
+
44
+ // "()"
45
+ // "(){}()"
46
+ // "({[()]})"
47
+ // "([])"
48
+ // "(([]){})"
49
+
50
+ console . log ( isValid ( "()" ) ) ;
You can’t perform that action at this time.
0 commit comments