forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheck If Word Is Valid After Substitutions.java
50 lines (35 loc) · 1.59 KB
/
Check If Word Is Valid After Substitutions.java
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
class Solution {
public boolean isValid(String s) {
//Lets see how we can solve that as we know we have only abc in string.
//Like aabcbc
// See as that ((b)b) Think a is '(' and c is ')'.
// If a string is made by using abc only we can remove abc to make it empty also.
//Think in Reverse Way.
Stack<Character> stack = new Stack<>();
char[] arr = s.toCharArray();
for (int i = 0; i < arr.length; i++) {
// We have to work only when we get ')' means c.
if(arr[i] == 'c')
{
// If we at c means we have 2 elements before us a and b.
// When we first pop we get b at second pop we get a
// If this all hold true we will delete a and b we are not adding c so c also
if(stack.size()>=2 && stack.pop() == 'b' && stack.pop() == 'a')
{
}
else
{
// If anywhere we get false in any condition that means this is not a valid set i.e. abc pattern is not present.
return false;
}
}
else
{
// For a and b we simply add.
stack.push(arr[i]);
}
}
//If we have only abc pattern the stack will become empty.
return stack.size()==0;
}
}