
- 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
Program to check we can replace characters to make a string to another string or not in C++
Suppose we have two lowercase strings s and t. Now consider an operation where we replace all occurrences of a character in s with another character. If we can perform this operation any number of times, we have to check whether s can be converted to t or not.
So, if the input is like s = "eye" t = "pip", then the output will be True, as we can replace "e"s with "p"s then "y" by "i".
To solve this, we will follow these steps −
Define one map m1 and another map m2
n := size of s
for initialize i := 0, when i < n, update (increase i by 1), do −
if s[i] is in m1, then −
if m1[s[i]] is same as t[i], then −
go for the next iteration
return false
Otherwise
m1[s[i]] := t[i]
return true
Example
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; bool solve(string s, string t) { map m1, m2; int n = s.size(); for(int i = 0; i <n; i++){ if(m1.count(s[i])){ if(m1[s[i]] == t[i]) continue; return false; } else{ m1[s[i]] = t[i]; } } return true; } int main(){ string s = "eye", t = "pip"; cout << solve(s, t); }
Input
"eye","pip"
Output
1
- Related Articles
- Program to check whether we can make k palindromes from given string characters or not in Python?
- C++ Program to check string can be reduced to 2022 or not
- Check if a string can be repeated to make another string in Python
- C# Program to check a string for whitespace characters or null
- Program to check string is palindrome with lowercase characters or not in Python
- Program to check whether we can convert string in K moves or not using Python
- Program to check whether one string swap can make strings equal or not using Python
- C# program to check if a string is palindrome or not
- Program to count number of unique palindromes we can make using string characters in Python
- C# program to check if string is panagram or not
- C++ Program to check string is strictly alphabetical or not
- C++ program to check we can make two strings equal by swapping from third string
- Program to check string contains consecutively descending string or not in Python
- Program to check the string is repeating string or not in Python
- C# program to check whether a given string is Heterogram or not

Advertisements