forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0678-valid-parenthesis-string.kt
57 lines (48 loc) · 2.65 KB
/
0678-valid-parenthesis-string.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Solution {
fun checkValidString(s: String): Boolean {
// number of opening parenthesis when '*' is taken as '('
var noOfOpenParenthesisWhenStarIsAnOpeningParenthesis = 0
// number of opening parenthesis when '*' is taken as ')'
var noOfOpenParenthesisWhenStarIsAClosingParenthesis = 0
// number of opening parenthesis when '*' is taken as ' '
var noOfOpenParenthesisWhenStarIsAnEmptyString = 0
for (char in s) {
if (char == '(') {
noOfOpenParenthesisWhenStarIsAnOpeningParenthesis++
noOfOpenParenthesisWhenStarIsAClosingParenthesis++
noOfOpenParenthesisWhenStarIsAnEmptyString++
}
if (char == '*') {
noOfOpenParenthesisWhenStarIsAnOpeningParenthesis++
noOfOpenParenthesisWhenStarIsAClosingParenthesis--
}
if (char == ')') {
noOfOpenParenthesisWhenStarIsAnOpeningParenthesis--
noOfOpenParenthesisWhenStarIsAClosingParenthesis--
noOfOpenParenthesisWhenStarIsAnEmptyString--
}
// A negative value indicates an excess of closing parenthesis.
// Eg: -1 indicates that there are no opening parenthesis and 1 closing parenthesis.
// If at least one of our possibilities is a positive value, then it indicates
// that the string is possibly valid.
// If all of our possibilities have a negative value, it indicates that none of the
// possibilities lead to a valid string. Hence, return false.
if (
noOfOpenParenthesisWhenStarIsAnOpeningParenthesis < 0 &&
noOfOpenParenthesisWhenStarIsAClosingParenthesis < 0 &&
noOfOpenParenthesisWhenStarIsAnEmptyString < 0
) return false
// Ensure that the variables are always positive. A negative value doesn't make sense
// because there cannot be a negative number of opening parenthesis.
noOfOpenParenthesisWhenStarIsAnOpeningParenthesis =
noOfOpenParenthesisWhenStarIsAnOpeningParenthesis.coerceAtLeast(0)
noOfOpenParenthesisWhenStarIsAClosingParenthesis =
noOfOpenParenthesisWhenStarIsAClosingParenthesis.coerceAtLeast(0)
noOfOpenParenthesisWhenStarIsAnEmptyString =
noOfOpenParenthesisWhenStarIsAnEmptyString.coerceAtLeast(0)
}
return noOfOpenParenthesisWhenStarIsAnOpeningParenthesis == 0 ||
noOfOpenParenthesisWhenStarIsAClosingParenthesis == 0 ||
noOfOpenParenthesisWhenStarIsAnEmptyString == 0
}
}