- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Converting one string to other using append and delete last operations in C++
In this tutorial, we will be discussing a program to convert one string to other using append and delete last operations.
For this we will be provided with two strings. Our task is to calculate whether the first string can be converted into the second one by performing k operations of append and delete last element.
Example
#include <bits/stdc++.h> using namespace std; //checking if conversion between strings is possible bool if_convert(string str1, string str2, int k){ if ((str1.length() + str2.length()) < k) return true; //finding common length of both string int commonLength = 0; for (int i = 0; i < min(str1.length(), str2.length()); i++) { if (str1[i] == str2[i]) commonLength++; else break; } if ((k - str1.length() - str2.length() + 2 * commonLength) % 2 == 0) return true; return false; } int main(){ str1 = "tutorials", str2 = "point"; k = 5; cout << endl; if (if_convert(str1, str2, k)) cout << "Yes"; else cout << "No"; return 0; }
Output
No
Advertisements