Skip to content

Commit 7ecb6ee

Browse files
committed
Add solution #678
1 parent 03c7199 commit 7ecb6ee

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@
511511
675|[Cut Off Trees for Golf Event](./0675-cut-off-trees-for-golf-event.js)|Hard|
512512
676|[Implement Magic Dictionary](./0676-implement-magic-dictionary.js)|Medium|
513513
677|[Map Sum Pairs](./0677-map-sum-pairs.js)|Medium|
514+
678|[Valid Parenthesis String](./0678-valid-parenthesis-string.js)|Medium|
514515
680|[Valid Palindrome II](./0680-valid-palindrome-ii.js)|Easy|
515516
684|[Redundant Connection](./0684-redundant-connection.js)|Medium|
516517
686|[Repeated String Match](./0686-repeated-string-match.js)|Easy|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
};

0 commit comments

Comments
 (0)