Skip to content

Commit

Permalink
add solution for #678 Valid Parenthesis String.
Browse files Browse the repository at this point in the history
  • Loading branch information
ecgan committed Apr 16, 2020
1 parent e43c760 commit 59eae80
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 0 deletions.
71 changes: 71 additions & 0 deletions problems/valid-parenthesis-string/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Valid Parenthesis String

LeetCode #: [678](https://leetcode.com/problems/valid-parenthesis-string/)

Difficulty: Medium.

Topics: String.

## Problem

Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules:

- Any left parenthesis `'('` must have a corresponding right parenthesis `')'`.
- Any right parenthesis `')'` must have a corresponding left parenthesis `'('`.
- Left parenthesis `'('` must go before the corresponding right parenthesis `')'`.
- `'*'` could be treated as a single right parenthesis `')'` or a single left parenthesis `'('` or an empty string.
- An empty string is also valid.

Example 1:

```text
Input: "()"
Output: True
```

Example 2:

```text
Input: "(*)"
Output: True
```

Example 3:

```text
Input: "(*))"
Output: True
```

Note:

- The string size will be in the range [1, 100].

## Solution Explanation

We need to check for pairs of matching left and right parenthesis. For this kind of question, we usually just need to keep track of number of left parentheses.

However, with the possible occurrence of the character `'*'`, there could be different number of left parentheses. We track it by using `minLeft` (minimum number of left paretheses) and `maxLeft` (possible maximum number of left paretheses). For example:

```text
Input: ((
minLeft: 2
maxLeft: 2
Input: ((**
minLeft: 2
maxLeft: 4
```

When we encounter a `')'`, we just need to minus 1 accordingly from `minLeft` and `maxLeft`.

The result is false when:

- `maxLeft` is less than 0 (i.e. the number of `')'` is more than `'('` and `'*'`); or,
- `minLeft` is not equal to 0 in the end (i.e. there is no exact matching pairs of left and right parentheses).

## Complexity Analysis

Space complexity: O(1) constant space with `minLeft` and `maxLeft` only.

Time complexity: O(n) by looping through each character once.
35 changes: 35 additions & 0 deletions problems/valid-parenthesis-string/checkValidString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @param {string} s
* @return {boolean}
*/
const checkValidString = function (s) {
let minLeft = 0
let maxLeft = 0

for (let i = 0; i < s.length; i++) {
const char = s[i]

if (char === '(') {
minLeft++
maxLeft++
}

if (char === '*') {
minLeft = Math.max(0, minLeft - 1)
maxLeft++
}

if (char === ')') {
minLeft = Math.max(0, minLeft - 1)
maxLeft--

if (maxLeft < 0) {
return false
}
}
}

return minLeft === 0
}

module.exports = checkValidString
81 changes: 81 additions & 0 deletions problems/valid-parenthesis-string/checkValidString.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const checkValidString = require('./checkValidString')

test('Example 1', () => {
const input = '()'

const result = checkValidString(input)

expect(result).toBe(true)
})

test('Example 2', () => {
const input = '(*)'

const result = checkValidString(input)

expect(result).toBe(true)
})

test('Example 3', () => {
const input = '(*))'

const result = checkValidString(input)

expect(result).toBe(true)
})

test('empty string is valid', () => {
const input = ''

const result = checkValidString(input)

expect(result).toBe(true)
})

test('one (', () => {
const input = '('

const result = checkValidString(input)

expect(result).toBe(false)
})

test('one )', () => {
const input = ')'

const result = checkValidString(input)

expect(result).toBe(false)
})

test('(()', () => {
const input = '(()'

const result = checkValidString(input)

expect(result).toBe(false)
})

test('())', () => {
const input = '())'

const result = checkValidString(input)

expect(result).toBe(false)
})

test('*)', () => {
const input = '*)'

const result = checkValidString(input)

expect(result).toBe(true)
})

test('(*', () => {
const input = '(*'

const result = checkValidString(input)

expect(result).toBe(true)
})

0 comments on commit 59eae80

Please sign in to comment.