Given a string s, determine if it is valid.

A string s is valid if, starting with an empty string t = "", you can transform t into s after performing the following operation any number of times:


	Insert string "abc" into any position in t. More formally, t becomes tleft + "abc" + tright, where t == tleft + tright. Note that tleft and tright may be empty.


Return true if s is a valid string, otherwise, return false.

&nbsp;
Example 1:

Input: s = "aabcbc"
Output: true
Explanation:
"" -&gt; "abc" -&gt; "aabcbc"
Thus, "aabcbc" is valid.

Example 2:

Input: s = "abcabcababcc"
Output: true
Explanation:
"" -&gt; "abc" -&gt; "abcabc" -&gt; "abcabcabc" -&gt; "abcabcababcc"
Thus, "abcabcababcc" is valid.


Example 3:

Input: s = "abccba"
Output: false
Explanation: It is impossible to get "abccba" using the operation.


&nbsp;
Constraints:


	1 &lt;= s.length &lt;= 2 * 104
	s consists of letters 'a', 'b', and 'c'


class Solution:
    def isValid(self, s: str) -> bool:
        
        
        ans = ''
        for i in s:
            ans+=i
            while len(ans)>=3:
                if ans[-3:]=="abc":
                    ans=ans[0:-3]
                else:
                    break
            
        if ans=='':
            return True
        else:
            return False
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;
    }
}
class Solution {
public:
    bool isValid(string s) {
        stack<char>st;
        for(int i=0;i<s.length();i++){
            if(s[i]=='a')
                st.push(s[i]);
            else if(s[i]=='b'){
                if(st.empty() || st.top()!='a')
                return false;
                else
                    st.push(s[i]);
            }
            else if(s[i]=='c'){
                if(st.empty() ||  st.top()!='b')
                    return false;
                else{
                    for(int i=0;i<2;i++)
                        st.pop();
                }
            }
        }
        return st.size()==0;
    }
};
var isValid = function(s) {
    const stack = [];
    for(let c of s) {
        if(c !== 'c') {
            stack.push(c);
        }else {
            if(stack.pop() !== 'b') return false;
            if(stack.pop() !== 'a') return false;
        }
    }

    return stack.length === 0;
};
Check If Word Is Valid After Substitutions