Skip to content

Commit 29aedc7

Browse files
committed
Add solution #1003
1 parent c98d8e7 commit 29aedc7

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,081 LeetCode solutions in JavaScript
1+
# 1,082 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -811,6 +811,7 @@
811811
1000|[Minimum Cost to Merge Stones](./solutions/1000-minimum-cost-to-merge-stones.js)|Hard|
812812
1001|[Grid Illumination](./solutions/1001-grid-illumination.js)|Hard|
813813
1002|[Find Common Characters](./solutions/1002-find-common-characters.js)|Easy|
814+
1003|[Check If Word Is Valid After Substitutions](./solutions/1003-check-if-word-is-valid-after-substitutions.js)|Medium|
814815
1004|[Max Consecutive Ones III](./solutions/1004-max-consecutive-ones-iii.js)|Medium|
815816
1005|[Maximize Sum Of Array After K Negations](./solutions/1005-maximize-sum-of-array-after-k-negations.js)|Easy|
816817
1009|[Complement of Base 10 Integer](./solutions/1009-complement-of-base-10-integer.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 1003. Check If Word Is Valid After Substitutions
3+
* https://leetcode.com/problems/check-if-word-is-valid-after-substitutions/
4+
* Difficulty: Medium
5+
*
6+
* Given a string s, determine if it is valid.
7+
*
8+
* A string s is valid if, starting with an empty string t = "", you can transform t into s after
9+
* performing the following operation any number of times:
10+
* - Insert string "abc" into any position in t. More formally, t becomes tleft + "abc" + tright,
11+
* where t == tleft + tright. Note that tleft and tright may be empty.
12+
*
13+
* Return true if s is a valid string, otherwise, return false.
14+
*/
15+
16+
/**
17+
* @param {string} s
18+
* @return {boolean}
19+
*/
20+
var isValid = function(s) {
21+
const stack = [];
22+
23+
for (const char of s) {
24+
if (char === 'c') {
25+
if (stack.length < 2 || stack.pop() !== 'b' || stack.pop() !== 'a') {
26+
return false;
27+
}
28+
} else {
29+
stack.push(char);
30+
}
31+
}
32+
33+
return stack.length === 0;
34+
};

0 commit comments

Comments
 (0)