- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# C++ program to check we can make two strings equal by swapping from third string

Suppose we have three strings S, T and U of same length n. For every index i in range 0 to n-1, we must swap U[i] with either S[i] or T[i]. So in total we have performed n swapping operations. We have to check whether after such n operations we can make string S exactly same as T.

So, if the input is like S = "abc"; T = "bca"; U = "bca", then the output will be True, because for all i if we swap U[i] with S[i], it will be "bca", and T is already "bca".

## Steps

To solve this, we will follow these steps −

for initialize i := 0, when S[i] is non-zero, update (increase i by 1), do: if S[i] is not equal to U[i] and T[i] is not equal to U[i], then: return false return true

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; bool solve(string S, string T, string U) { for (int i = 0; S[i]; ++i) if (S[i] != U[i] && T[i] != U[i]) return false; return true; } int main() { string S = "abc"; string T = "bca"; string U = "bca"; cout << solve(S, T, U) << endl; }

## Input

"abc", "bca", "bca"

## Output

1

- Related Questions & Answers
- Program to check two strings can be equal by swapping characters or not in Python
- Program to equal two strings of same length by swapping characters in Python
- Program to check whether one string swap can make strings equal or not using Python
- Program to check whether we can make group of two partitions with equal sum or not in Python?
- Program to check whether two trees can be formed by swapping nodes or not in Python
- Program to check we can make arithmetic progression from sequence in Python
- Program to check whether we can make k palindromes from given string characters or not in Python?
- How to check if two strings are equal in Java?
- Program to check we can replace characters to make a string to another string or not in C++
- Check if it is possible to make two matrices strictly increasing by swapping corresponding values only in Python
- Program to find possible number of palindromes we can make by trimming string in Python
- Swapping two variable value without using third variable in C/C++
- Concatenate strings from two fields into a third field in MongoDB?
- Check if two strings are equal or not in Arduino
- Minimum Swaps to Make Strings Equal in C++

Advertisements