
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to check whether one string can be 1-to-1 mapped into another string in Python
Suppose we have two lowercase strings s, and t we have to check whether we can create one 1-to-1 mapping for each letter in s to another letter (may be the same letter) such that s can be mapped to t. (The ordering of characters will not be changed).
So, if the input is like s = "papa", t = "lili", then the output will be True, as we can create this mapping: "p" to "l", "a" -> "i"
To solve this, we will follow these steps −
- s_dict := a new map
- t_dict := a new map
- for i in range 0 to minimum of s size and t size, do
- if s[i] is present in s_dict, then
- if s_dict[s[i]] is not same as t[i], then
- return False
- if s_dict[s[i]] is not same as t[i], then
- otherwise when t[i] is present in t_dict, then
- if t_dict[t[i]] is not same as s[i], then
- return False
- if t_dict[t[i]] is not same as s[i], then
- otherwise,
- s_dict[s[i]] := t[i]
- t_dict[t[i]] := s[i]
- if s[i] is present in s_dict, then
- return True
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, s, t): s_dict = {} t_dict = {} for i in range(min(len(s), len(t))): if s[i] in s_dict: if s_dict[s[i]] != t[i]: return False elif t[i] in t_dict: if t_dict[t[i]] != s[i]: return False else: s_dict[s[i]] = t[i] t_dict[t[i]] = s[i] return True ob = Solution() print(ob.solve("papa", "lili"))
Input
"papa", "lili"
Output
True
- Related Articles
- Program to check one string can be converted to another by removing one element in Python
- Program to check whether one point can be converted to another or not in Python
- Check if a string can be repeated to make another string in Python
- Java Program to check whether one String is a rotation of another.
- Program to check whether we can split a string into descending consecutive values in Python
- Program to check whether one string swap can make strings equal or not using Python
- Program to check a string can be split into three palindromes or not in Python
- Program to check one string can be converted to other by shifting characters clockwise in Python
- Check whether second string can be formed from characters of first string in Python
- Check if a string can be converted to another string by replacing vowels and consonants in Python
- Program to check whether final string can be formed using other two strings or not in Python
- Check if a string can be formed from another string using given constraints in Python
- Check if a string can be obtained by rotating another string 2 places in Python
- Java Program to Insert a string into another string
- Golang program to insert a string into another string

Advertisements