
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Check If Word Is Valid After Substitutions in C++
Suppose we are given that the string "abc" is valid. So from any valid string V, we can divide V into two pieces X and Y such that X + Y is same as V. (X or Y may be empty.). Then, X + "abc" + Y is also valid. So for example S = "abc", then examples of valid strings are: "abc", "aabcbc", "abcabc", "abcabcababcc". And some examples of invalid strings are: "abccba", "ab", "cababc", "bac". We have to check true if and only if the given string S is valid.
So if the input is like “abcabcababcc”, then that is valid, so output will be true.
To solve this, we will follow these steps −
define a stack st
for i in range 0 to size of S
if st is empty or S[i] is not same as ‘c’, then push S[i] into stack
otherwise
insert c into the st
while size of st >= 3
c := top of st and pop the top element from st
b := top of st and pop the top element from st
a := top of st and pop the top element from st
temp := temp concatenate with a
temp := temp concatenate with b
temp := temp concatenate with c
if temp is “abc”, then go for next iteration
otherwise
push a, then b, then c into st, and break the loop
return true, when the st is empty, otherwise return false
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: bool isValid(string S) { stack <char> st; for(int i = 0; i < S.size(); i++){ if(st.empty() || S[i] != 'c'){ st.push(S[i]); }else{ st.push('c'); while(st.size() >= 3){ char c = st.top(); st.pop(); char b = st.top(); st.pop(); char a = st.top(); st.pop(); string temp = ""; temp += a; temp += b; temp += c; if(temp == "abc"){ continue; }else{ st.push(a); st.push(b); st.push(c); break; } } } } return st.empty(); } }; main(){ Solution ob; cout << (ob.isValid("abcabcababcc")); }
Input
“abcabcababcc”
Output
1
- Related Articles
- Check if a + b = c is valid after removing all zeroes from a, b and c in C++
- Check if a given string is a valid number in Python
- Check if a given string is a valid number in C++
- How to check if a string is a valid keyword in Python?
- How to check if a string is a valid keyword in C#?
- How to check if a string is a valid keyword in Java?
- How to check if a string is a valid URL in Golang?
- Finding all valid word squares in JavaScript
- Java Program to check if a string is a valid number
- C Program to check if a date is valid or not
- Check whether triangle is valid or not if sides are given in Python
- How To Check If The Email Address Is Valid Or Not In Excel?
- How to check if an URL is valid or not using Java?
- Python Program to Check if a Date is Valid and Print the Incremented Date if it is
- Check if a string starts with given word in PHP
