File tree 2 files changed +34
-0
lines changed
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 511
511
675|[ Cut Off Trees for Golf Event] ( ./0675-cut-off-trees-for-golf-event.js ) |Hard|
512
512
676|[ Implement Magic Dictionary] ( ./0676-implement-magic-dictionary.js ) |Medium|
513
513
677|[ Map Sum Pairs] ( ./0677-map-sum-pairs.js ) |Medium|
514
+ 678|[ Valid Parenthesis String] ( ./0678-valid-parenthesis-string.js ) |Medium|
514
515
680|[ Valid Palindrome II] ( ./0680-valid-palindrome-ii.js ) |Easy|
515
516
684|[ Redundant Connection] ( ./0684-redundant-connection.js ) |Medium|
516
517
686|[ Repeated String Match] ( ./0686-repeated-string-match.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 678. Valid Parenthesis String
3
+ * https://leetcode.com/problems/valid-parenthesis-string/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given a string s containing only three types of characters: '(', ')' and '*', return
7
+ * true if s is valid.
8
+ *
9
+ * The following rules define a valid string:
10
+ * - Any left parenthesis '(' must have a corresponding right parenthesis ')'.
11
+ * - Any right parenthesis ')' must have a corresponding left parenthesis '('.
12
+ * - Left parenthesis '(' must go before the corresponding right parenthesis ')'.
13
+ * - '*' could be treated as a single right parenthesis ')' or a single left parenthesis
14
+ * '(' or an empty string "".
15
+ */
16
+
17
+ /**
18
+ * @param {string } s
19
+ * @return {boolean }
20
+ */
21
+ var checkValidString = function ( s ) {
22
+ let result = 0 ;
23
+ let offset = 0 ;
24
+
25
+ for ( const character of s ) {
26
+ result += character === '(' ? 1 : - 1 ;
27
+ offset += character !== ')' ? 1 : - 1 ;
28
+ if ( offset < 0 ) return false ;
29
+ result = Math . max ( 0 , result ) ;
30
+ }
31
+
32
+ return ! result ;
33
+ } ;
You can’t perform that action at this time.
0 commit comments