- 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
Check if a permutation of S2 can be obtained by adding or removing characters from S1
Checking if a permutation of S2 can be obtained by adding or removing characters from S1 is a common problem in computer science. This problem is of great significance in various domains, including data processing, text analysis, and pattern recognition.
In this tutorial, we will be presenting a solution to this problem using C++ programming language. The approach involves analyzing the characteristics of S1 and S2 to establish whether S2 can be rearranged to form a permutation of S1. We will be providing the C++ code for this approach along with explanations to help readers understand the problem and solution better.
By the end of this tutorial, you will have a comprehensive understanding of how to approach the challenge of checking if a permutation of S2 can be obtained by adding or removing characters from S1. So let’s get started!
Problem Statement
Given two strings S1 and S2, the task is to check if S1 can be a permutation of S2 by adding or removing characters from S1. Print "A permutation of S2 can be obtained by adding or removing characters from S1.", if it's feasible; otherwise, print "A permutation of S2 cannot be obtained by adding or removing characters from S1."
Note: A permutation of a string refers to a rearrangement of its characters.
Now, let’s understand this problem statement with examples.
Sample Example 1
Input
"listen"; S2 = "silent"
Output
A permutation of S2 can be obtained by adding or removing characters from S1.
Explanation: The string S1 can be transformed into S2 by removing the character 't' and adding the character 'i' at its correct position. Hence, it is possible for S1 to become a permutation of S2 through the addition or removal of characters from S1.
Sample Example 2
Input
S1 = "hello"; S2 = "world";
Output
A permutation of S2 cannot be obtained by adding or removing characters from S1.
Explanation: The string S1 cannot be transformed into S2 by adding or removing characters from S1. As a result, it is not possible for S1 to be a permutation of S2 through the addition or removal of characters.
Algorithm
1. Define a function ‘checkPermutation’ that takes two strings, ‘s1’ and ‘s2’, as input.
2. Inside the checkPermutation function, create copies of ‘s1’ and ‘s2’ and assign them to ‘sorted_s1’ and ‘sorted_s2’ variables, respectively.
3. Sort the ‘sorted_s1’ and ‘sorted_s2’ strings using the ‘std::sort’ function from the ‘<algorithm>’ library.
4. Compare the sorted strings using the ‘==’ operator to check if they are equal.
5. If the sorted strings are equal, return true from the ‘checkPermutation’ function, indicating that a permutation of ‘s2’ can be obtained by adding or removing characters from ‘s1’.
6. If the sorted strings are not equal, return ‘false’ from the ‘checkPermutation’ function, indicating that a permutation of ‘s2’ cannot be obtained by adding or removing characters from ‘s1’.
7. In the ‘main’ function, define the test strings ‘s1’ and ‘s2’ with appropriate values.
8. Call the ‘checkPermutation’ function with ‘s1’ and ‘s2’ as arguments.
9. Based on the return value of the ‘checkPermutation’ function, display an appropriate message indicating whether a permutation of ‘s2’ can be obtained by adding or removing characters from ‘s1’
The algorithm follows a simple approach of sorting the strings and comparing them to determine if they are permutations of each other. Sorting allows us to easily identify if the characters present in one string are a subset of the other string. Now, let’s understand it with the help of an example using C++.
Example
Implementation of the above algorithm using C++
The below C++ program defines a function ‘checkPermutation’ that takes two strings, ‘s1’ and ‘s2’, as input. It creates copies of the strings and sorts them. Then, it compares the sorted strings to check if they are equal. If they are equal, it means a permutation of ‘s2’ can be obtained by adding or removing characters from ‘s1’.
Input
S1 = "listen"; S2 = "silent";
Expected Output
Expected Output: A permutation of S2 can be obtained by adding or removing characters from S1.
Example
#include <iostream> #include <string> #include <algorithm> bool checkPermutation(const std::string& s1, const std::string& s2) { // Create copies of the strings and sort them std::string sorted_s1 = s1; std::string sorted_s2 = s2; std::sort(sorted_s1.begin(), sorted_s1.end()); std::sort(sorted_s2.begin(), sorted_s2.end()); // Check if the sorted strings are equal return sorted_s1 == sorted_s2; } int main() { std::string s1 = "listen"; std::string s2 = "silent"; if (checkPermutation(s1, s2)) { std::cout << "A permutation of S2 can be obtained by adding or removing characters from S1.\n"; } else { std::cout << "A permutation of S2 cannot be obtained by adding or removing characters from S1.\n"; } return 0; }
Output
A permutation of S2 can be obtained by adding or removing characters from S1.
Conclusion
In this tutorial, we have learned the problem of checking if a permutation of S2 can be obtained by adding or removing characters from S1. Also, we developed an efficient algorithm based on sorting. By comparing the sorted versions of the strings, we can determine if their characters’ match, regardless of the order or additional characters. We hope this tutorial has helped you to confidently approach and solve problems involving permutations and string manipulation.