Check If Word Is Valid After Substitutions

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 159:-Check If Word Is Valid After Substitutions

The problem of checking if a given string is valid after applying certain substitutions can be solved using a stack. In this problem, you are given a string containing only the characters 'a', 'b', and 'c'. You can perform the following operation: choose any two adjacent characters and replace them with "c" if they are "ab" or "bc". The goal is to determine if the given string can be made valid after performing zero or more such operations.

Here's how you can approach this problem:

  1. Iterate through the characters of the string.
  2. For each character, if the stack is not empty and the top two characters of the stack are 'a' and 'b', pop the top two characters.
  3. If the above step was not performed, push the current character onto the stack.
  4. After iterating through all characters, if the stack is empty, the string is valid; otherwise, it is not.

Here's a C++ implementation to check if a given string is valid after substitutions:

 

cppCopy code

#include <iostream> #include <stack> bool isValidAfterSubstitutions(const std::string& s) { std::stack<char> charStack; for (char ch : s) { if (!charStack.empty() && charStack.top() == 'a' && ch == 'b') { charStack.pop(); } else if (!charStack.empty() && charStack.top() == 'b' && ch == 'c') { charStack.pop(); } else { charStack.push(ch); } } return charStack.empty(); } int main() { std::string input = "abcab"; bool isValid = isValidAfterSubstitutions(input); if (isValid) { std::cout << "The string is valid after substitutions." << std::endl; } else { std::cout << "The string is not valid after substitutions." << std::endl; } return 0; }

In this example, the isValidAfterSubstitutions function checks whether the given string is valid after applying the described substitutions. The main function demonstrates how to use this function for a specific input string.

For the input "abcab", the output will be "The string is valid after substitutions," as the string can be made valid by replacing the adjacent characters "ab" with "c".

21. Stacks - Assignments

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?