w3resource

Java: Check if two specified strings are isomorphic or not

Java Basic: Exercise-185 with Solution

Write a Java program to check if two strings are isomorphic or not.

Two strings are called isomorphic if the letters in one string can be remapped to get the second string. Remapping a letter means replacing all occurrences of it with another letter but the ordering of the letters remains unchanged. No two letters may map to the same letter, but a letter may map to itself.
Example-1: The words "abca" and "zbxz" are isomorphic because we can map 'a' to 'z', 'b' to 'b' and 'c' to 'x'.
Example-1: The words "bar" and "foo" are not isomorphic because we can map 'f' to 'b', 'o' to 'a' and 'o' to 'r'.

Sample Solution:

Java Code:

// Importing necessary Java utilities
import java.util.*;

// Main class Solution
public class Solution {
    // Main method
    public static void main(String[] args) {
        // Declaring and initializing two strings
        String str1 = "abca";
        String str2 = "zbxz";
		
		// Printing if the two strings are isomorphic or not
        System.out.println("Is " + str1 + " and " + str2 + " are Isomorphic? " + is_Isomorphic(str1, str2));
    }

    // Method to check if two strings are isomorphic
    public static boolean is_Isomorphic(String str1, String str2) {
        // Check for invalid inputs or unequal lengths of strings
        if (str1 == null || str2 == null || str1.length() != str2.length())
            return false;
        
        // Creating a HashMap to store character mappings
        Map<Character, Character> map = new HashMap<>();
        
        // Loop through each character in the strings
        for (int i = 0; i < str1.length(); i++) {
            char char_str1 = str1.charAt(i), char_str2 = str2.charAt(i);
            
            // If the mapping for str1 character already exists
            if (map.containsKey(char_str1)) {
                // Check if the mapping matches with the corresponding character in str2
                if (map.get(char_str1) != char_str2)
                    return false;
            } else {
                // If no mapping for str1 character exists, check if str2 character is already mapped to another str1 character
                if (map.containsValue(char_str2))
                    return false;
                
                // Create a new mapping for str1 character to str2 character
                map.put(char_str1, char_str2);
            }
        }
        
        // If no discrepancies found, return true (strings are isomorphic)
        return true;
    }
}

Sample Output:

Is abca and zbxz are Isomorphic? true

Flowchart:

Flowchart: Java exercises: Check if two specified strings  are isomorphic or not.

Java Code Editor:

Company:  LinkedIn

Contribute your code and comments through Disqus.

Previous: Write a Java program to find the length of the longest consecutive sequence path of a given binary tree.
Next: Write a Java program to check if a number is a strobogrammatic number. The number is represented as a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.