×

Isomorphic String in Java

In this tutorial, we will understand what is meant by isomorphic String in java. We will also see a Java program to find out if the string is isomorphic or not. It is one of the popular interview questions for top-rated IT companies like Amazon, Accenture, Google, etc. This problem enhances the thinking power and problem-solving ability of students. The intention is to check the thinking – critical, and logical of the student.

Isomorphic String

The string is referred to as isomorphic if the letters of one string can be mapped to obtain the second string. By Mapping, we mean replacing all existences of a letter with another letter without changing the order of the letters.It should benoted that no two letters may map to the identical letter but a letter may map to itself.

Now, let us try to grasp it through an example.

Let us consider, string1 as DEDFE and string2 as WVZ. Now try to map string1 to string2.

CharacterMapping
DW
EV
FZ

Replace the letters of the string

D ->W, E ->V, D ->W, F -> Z, E ->V.

Recalling the definition of isomorphic, we observe that string1 and string2 are isomorphic to one another.

Example 2:

Let us consider, string1 is EFEFGE and string2 is WVZU. Now try to map string1 to string2.

CharacterMapping
EW
FV
GZ
EU

Replacing the letters,

E ->W, F ->V, E ->W, F -> V, G ->Z, E ->U.

Recalling the definition, we can say that string1 and string2 are not isomorphic to one another.

Example 3:

Let us consider, string1 as SERVICE and string2 asTUVWXYZ. Now try to map string1 to string2.

CharacterMapping
ST
EU
RV
VW
IX
CY
EZ

Replacing the letters,

S ->T, E ->U, R ->V, I ->X, C ->Y, E ->Z.

Recalling the definition, we can say that string1 and string2 are not isomorphic to one another. Two Es in string 1 is mapped with two different letters in string 2.

Set of Rules

  1. A mapping table is to be taken into accountthat would map every character from the first string to the only character in the second string.
  2. Another table is considered; call it mapped before the table to record every character from the second string which is already connected to a character from the second string.
  3. The first stringis read character by character.
  4. Get the mapping if the character gets presented in the mapping table.
  5. The equivalent character from the second string is read.
  6. Return false if the mapping and the studied character from the 2nd string are different.
  7. Otherwise, check if the studied character from the first string does not exist in the mapping table and take the following action:
    1. Read the equivalent character from the subsequent string.
    1. Return false if the character resides in the table named mappedBefore.
    1. Otherwise, include the read character from the first table and the read character from the second table in the mapping table.
  8. Go to step no. 3 and continue.
  9. Return true.

Implementation:

Now, let us see a java program to understand the explained concept.

import java.util.*;  
// creation of the class named IsomorphicString
public class IsomorphicString
{    


public static void main(String args[])   
{  
//strings to check whether they are isomorphic in nature or not    


String strg1 = "SERVICE", strg2 = "TUVWXYZ";  


System.out.println("Are "+strg1 +" and "+strg2 +" Isomorphic? "+isIsomorphicString(strg1, strg2));  


}  
//function to check if the given string is isomorphic or not  


public static booleanisIsomorphicString(String strg1, String strg2)   
{  


if (strg1 == null || strg2 == null || strg1.length() != strg2.length())  


//it returns false if string1 and string2 are null and of varying lengths
return false;  


//creation of an instance of Map  
Map<Character, Character> map = new HashMap<>();  
for (int j = 0; j < strg1.length(); j++)   
{  
//finding the character of the string at i-th position  
char char_strg1 = strg1.charAt(j), char_strg2 = strg2.charAt(j);  
//checks whether a particular key (i-th character) is being mapped into the Map or not  


if (map.containsKey(char_strg1))   
{  
if (map.get(char_strg1) != char_strg2)  
//returns false if the character of the first string and the character of the second string are not equal
return false;  
}  
else   
{  
//checking if the specific keyi.e (i-th character) is being mapped into the Map or not      
if (map.containsValue(char_strg2))            
return false;  
//adding key and value to the map  
.put(char_strg1, char_strg2);  
}  
}  
return true;  
}  
}  

Output:

Isomorphic String in Java

Explanation: The given two strings are not isomorphic as it violates the definition of an isomorphic string. There are two E in string 1 mapped to different letters in string 2.


Related Topics

How to Create Singleton Class in Java

In this tutorial, we will discuss the singleton class and how to create it. Introduction Java is a purely object-oriented programming language. It consists of classes and objects. But Singleton is one...

4 minutes read.

Java Switch Keyword

In this article we are going to learn the concept of a java switch keyword. Generally, java case keyword is used with the switch statements or keyword.Switch keyword is implemented in...

3 minutes read.

What is new in Java 17?

Java 17 LTS is the most recent long-term support release for the Java SE platform. Under the Oracle No-Fee Terms and Conditions License, which stands for long-term support, JDK 17...

6 minutes read.

Spliterator in Java 8

In this tutorial, we will understand the meaning of spliterator in java 8. It is just like any other iterator available in java used to traverse the elements of either...

4 minutes read.

How to Convert Object to String in Java

How to Convert Object to String in Java You can convert any Object to String in Java whether it is a user-defined class, StringBuilder or StringBuffer, etc. There are two methods...

2 minutes read.

How to encrypt password in Java

Every software program needs a username and password to identify a legitimate user. A username can be any number of things, including an email address or a string of characters....

6 minutes read.

Iccanobif Numbers in Java

In this article, you will be very well equipped with the knowledge of iccanobif numbers in Java. You will also know how they are formed and basic example programs on...

3 minutes read.

How to use Lambda Expression in Java?

The new and significant lambda expression feature of Java was added in Java SE 8. It provides a clear and concise mechanism for describing a single method interface using an...

4 minutes read.

Java Image

For all other classes used for representing graphical images, Java's Image class serves as an abstract superclass. For images in Java, a specific form of object known as a BufferedImage...

4 minutes read.

Chromatic Number in Java

The chromatic number is the bare minimum of colours necessary to accurately colour any graph. To put it another way, the chromatic number can be thought of as the bare...

6 minutes read.

Awesome explanation of Strings in Java

What do you mean by String? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

URLConnection Class

What is the URL? URL stands for Uniform Resource Locator, is used to specify addresses on the World Wide Web. A URL relates to the identification of any resource connected to the web. URL syntax: Protocol://hostname/other_information(files...

6 minutes read.

Vectors in Java

Vector Class We may make resizable arrays comparable to the ArrayList class using the Vector class, which implements the List interface. A vector is similar to a dynamic collection that can...

4 minutes read.

PriorityQueue in Java

PriorityQueue in Java A PriorityQueue is a member of the Java Collection Framework and is used when the values are needed to be processed based on priority. Priority queue operates similar to...

8 minutes read.

Minimum Number of Platforms Required for a Railway Station

The train station problem is one of the most significant problems typically posed in the programming round interview to gauge a candidate's aptitude for logic and problem-solving. Problem of Railway Station The...

6 minutes read.

Difference between throw and throws in java

This article shows you the core difference between “throw” and “throws”keywords in Java programming language.The throw keyword tells Java you want another part of the code to deal withthe exception,...

2 minutes read.

Java String equals() method

equals() method compares two string based on the their content. Syntax public boolean equals(Objects anObject) parameter anObject: Object to be compared with the current String. Returns It returns true when the current String is equivalent to...

1 minute read.

Singleton class in Java

What is Singleton class in Java? Singleton means it is one. That means we can create only one instance or object of a class. For example, let us have a class...

3 minutes read.

Java Integer class

The Integer class wraps a primitive int type value in an object. Its object contains only a single field whose type is int. Methods: The java.lang.Integer class provides several different methods for...

4 minutes read.

Java Arrays

An array is an object that stores a collection of values. An array can store two types of collection data: objects and primitives. An array is a collection of values which...

2 minutes read.