
- 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
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 Articles
- Program to check two strings can be equal by swapping characters or not in Python
- Check if two binary strings can be made equal by swapping 1s occurring before 0s
- 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 we can replace characters to make a string to another string or not in C++
- 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?
- Swapping two variable value without using third variable in C/C++
- Write a program in C++ to check if a string can be obtained by rotating another string by two places
- Minimum Swaps to Make Strings Equal in C++
- Meta Strings (Check if two strings can become same after a swap in one string) in C++
- Check if a given string can be formed by two other strings or their permutations
- Minimum number of given operations required to make two strings equal using C++.

Advertisements