# Check whether two strings can be made equal by copying their characters with the adjacent ones

Given two strings **str1** and **str2**, the task is to check whether both of the string can be made equal by copying any character of the string with its adjacent character. **Note** that this operation can be performed any number of times.

**Examples:**

Input:str1 = “abc”, str2 = “def”

Output:No

As all the characters in both the string are different.

So, there is no way they can be made equal.

Input:str1 = “abc”, str2 = “fac”

Output:Yes

str1 = “abc” -> “aac”

str2 = “fac” -> “aac”

**Approach:** In order for the strings to be made equal with the given operation, they have to be of equal lengths and there has to be at least one character which is common in both the strings. To check that, create a frequency array **freq[]** which will store the frequency of all the characters of **str1** and then for every character of **str2** if its frequency in **str1** is greater than **0** then it is possible to make both the strings equal.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define MAX 26 ` ` ` `// Function that returns true if both ` `// the strings can be made equal ` `// with the given operation ` `bool` `canBeMadeEqual(string str1, string str2) ` `{ ` ` ` `int` `len1 = str1.length(); ` ` ` `int` `len2 = str2.length(); ` ` ` ` ` `// Lengths of both the strings ` ` ` `// have to be equal ` ` ` `if` `(len1 == len2) { ` ` ` ` ` `// To store the frequency of the ` ` ` `// characters of str1 ` ` ` `int` `freq[MAX]; ` ` ` `for` `(` `int` `i = 0; i < len1; i++) { ` ` ` `freq[str1[i] - ` `'a'` `]++; ` ` ` `} ` ` ` ` ` `// For every character of str2 ` ` ` `for` `(` `int` `i = 0; i < len2; i++) { ` ` ` ` ` `// If current character of str2 ` ` ` `// also appears in str1 ` ` ` `if` `(freq[str2[i] - ` `'a'` `] > 0) ` ` ` `return` `true` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str1 = ` `"abc"` `, str2 = ` `"defa"` `; ` ` ` ` ` `if` `(canBeMadeEqual(str1, str2)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `class` `GFG ` `{ ` ` ` `static` `int` `MAX = ` `26` `; ` ` ` ` ` `// Function that returns true if both ` ` ` `// the strings can be made equal ` ` ` `// with the given operation ` ` ` `static` `boolean` `canBeMadeEqual(String str1, ` ` ` `String str2) ` ` ` `{ ` ` ` `int` `len1 = str1.length(); ` ` ` `int` `len2 = str2.length(); ` ` ` ` ` `// Lengths of both the strings ` ` ` `// have to be equal ` ` ` `if` `(len1 == len2) ` ` ` `{ ` ` ` ` ` `// To store the frequency of the ` ` ` `// characters of str1 ` ` ` `int` `freq[] = ` `new` `int` `[MAX]; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < len1; i++) ` ` ` `{ ` ` ` `freq[str1.charAt(i) - ` `'a'` `]++; ` ` ` `} ` ` ` ` ` `// For every character of str2 ` ` ` `for` `(` `int` `i = ` `0` `; i < len2; i++) ` ` ` `{ ` ` ` ` ` `// If current character of str2 ` ` ` `// also appears in str1 ` ` ` `if` `(freq[str2.charAt(i) - ` `'a'` `] > ` `0` `) ` ` ` `return` `true` `; ` ` ` `} ` ` ` `} ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `String str1 = ` `"abc"` `, str2 = ` `"defa"` `; ` ` ` ` ` `if` `(canBeMadeEqual(str1, str2)) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `MAX` `=` `26` ` ` `# Function that returns true if both ` `# the strings can be made equal ` `# with the given operation ` `def` `canBeMadeEqual(str1, str2): ` ` ` `len1 ` `=` `len` `(str1) ` ` ` `len2 ` `=` `len` `(str2) ` ` ` ` ` `# Lengths of both the strings ` ` ` `# have to be equal ` ` ` `if` `(len1 ` `=` `=` `len2): ` ` ` ` ` `# To store the frequency of the ` ` ` `# characters of str1 ` ` ` `freq ` `=` `[` `0` `for` `i ` `in` `range` `(` `MAX` `)] ` ` ` `for` `i ` `in` `range` `(len1): ` ` ` `freq[` `ord` `(str1[i]) ` `-` `ord` `(` `'a'` `)] ` `+` `=` `1` ` ` ` ` `# For every character of str2 ` ` ` `for` `i ` `in` `range` `(len2): ` ` ` ` ` `# If current character of str2 ` ` ` `# also appears in str1 ` ` ` `if` `(freq[` `ord` `(str2[i]) ` `-` `ord` `(` `'a'` `)] > ` `0` `): ` ` ` `return` `True` ` ` ` ` `return` `False` ` ` `# Driver code ` `str1 ` `=` `"abc"` `str2 ` `=` `"defa"` ` ` `if` `(canBeMadeEqual(str1, str2)): ` ` ` `print` `(` `"Yes"` `) ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

**Output:**

No

## Recommended Posts:

- Check whether two strings can be made equal by increasing prefixes
- Count of strings where adjacent characters are of difference one
- Check if a given string is made up of two alternating characters
- Check whether two strings contain same characters in same order
- Program to check if first and the last characters of string are equal
- Print array of strings in sorted order without copying one string into another
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Permutation of a string with maximum number of characters greater than its adjacent characters
- String with k distinct characters and no same characters adjacent
- Find the character made by adding all the characters of the given string
- Print all possible strings that can be made by placing spaces
- Print all possible strings that can be made by placing spaces
- Count number of strings (made of R, G and B) using given combination
- Find the longest string that can be made up of other strings from the array
- Rearrange characters in a string such that no two adjacent are same

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.