Skip to content

Commit 6f57360

Browse files
havanagrawalfishercoder1534
authored andcommitted
Add solution for 1003 (fishercoder1534#50)
1 parent a2fd400 commit 6f57360

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Your ideas/fixes/algorithms are more than welcome!
3434
|1018|[Binary Prefix Divisible By 5](https://leetcode.com/problems/binary-prefix-divisible-by-5/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1018.java) | O(n) | O(1) | |Easy|
3535
|1013|[Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1013.java) | O(n) | O(1) | |Easy|
3636
|1009|[Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1009.java) | O(n) | O(1) | |Easy|
37+
|1003|[Check If Word Is Valid After Substitutions](https://leetcode.com/problems/check-if-word-is-valid-after-substitutions/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1003.java) | O(n) | O(n) | |Medium|
3738
|1002|[Find Common Characters](https://leetcode.com/problems/find-common-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1002.java) | O(n) | O(1) | |Easy|
3839
|999|[Available Captures for Rook](https://leetcode.com/problems/available-captures-for-rook/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_999.java) | O(1) | O(1) | |Easy|
3940
|997|[Find the Town Judge](https://leetcode.com/problems/find-the-town-judge/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_997.java) | O(n) | O(n) | |Easy|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
/**
7+
* 1003. Check If Word Is Valid After Substitutions
8+
*
9+
* We are given that the string "abc" is valid.
10+
*
11+
* From any valid string V, we may split V into two pieces X and Y such that X + Y (X concatenated with Y) is equal to V. (X or Y may be empty.) Then, X + "abc" + Y is also valid.
12+
*
13+
* If for example S = "abc", then examples of valid strings are: "abc", "aabcbc", "abcabc", "abcabcababcc". Examples of invalid strings are: "abccba", "ab", "cababc", "bac".
14+
*
15+
* Return true if and only if the given string S is valid.
16+
*
17+
* Note:
18+
* * 1 <= S.length <= 20000
19+
* * S[i] is 'a', 'b', or 'c'
20+
*/
21+
22+
public class _1003 {
23+
public static class Solution1 {
24+
public boolean isValid(String S) {
25+
Deque<Character> stack = new ArrayDeque<>();
26+
27+
for (char c : S.toCharArray()) {
28+
if (c == 'c') {
29+
if (stack.isEmpty() || stack.pop() != 'b') return false;
30+
if (stack.isEmpty() || stack.pop() != 'a') return false;
31+
} else {
32+
stack.push(c);
33+
}
34+
}
35+
36+
return stack.isEmpty();
37+
}
38+
}
39+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1003;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertFalse;
8+
import static org.junit.Assert.assertTrue;
9+
10+
public class _1003Test {
11+
private static _1003.Solution1 solution1;
12+
13+
@BeforeClass
14+
public static void setup() {solution1 = new _1003.Solution1();}
15+
16+
@Test
17+
public void test1() {
18+
assertTrue(solution1.isValid("aabcbc"));
19+
}
20+
21+
@Test
22+
public void test2() {
23+
assertTrue(solution1.isValid("abcabcababcc"));
24+
}
25+
26+
@Test
27+
public void test3() {
28+
assertFalse(solution1.isValid("abccba"));
29+
}
30+
31+
@Test
32+
public void test4() {
33+
assertFalse(solution1.isValid("cababc"));
34+
}
35+
}

0 commit comments

Comments
 (0)