LeetCode-1003 Check If Word Is Valid After Substitutions Solution (with Java)

1. Description:

Notes:

2. Examples:

3.Solutions:

 1 /**
 2  * Created by sheepcore on 2019-05-07
 3  */
 4 class Solution {
 5     public boolean isValid(String s) {
 6         Stack<Character> stack = new Stack<>();
 7         for(char ch : s.toCharArray()){
 8             switch(ch){
 9                 case 'a':
10                 case 'b': stack.push(ch); break;
11                 case 'c':
12                     if(!stack.isEmpty() && stack.peek() == 'b')
13                         stack.pop();
14                     else
15                         return false;
16                     if(!stack.isEmpty() && stack.peek() == 'a')
17                         stack.pop();
18                     else
19                         return false;
20                     break;
21             }
22         }
23         return stack.isEmpty(); 
24     }
25 }

 

 

posted @ 2020-03-02 14:40  SheepCore  阅读(107)  评论(0编辑  收藏  举报