diff --git a/algorithms/Java/README.md b/algorithms/Java/README.md index 70c32fad..09ffeb53 100644 --- a/algorithms/Java/README.md +++ b/algorithms/Java/README.md @@ -88,6 +88,7 @@ - [Boyer Moore Search](strings/Boyer_Moore.java) - [Reverse String](strings/reverse-string.java) - [First Non Repeating Character](strings/first-non-repeating-char.java) +- [Isomorphic Strings](strings/isomorphic_strings.java) ## Trees diff --git a/algorithms/Java/strings/isomorphic_strings.java b/algorithms/Java/strings/isomorphic_strings.java new file mode 100644 index 00000000..5079c21c --- /dev/null +++ b/algorithms/Java/strings/isomorphic_strings.java @@ -0,0 +1,61 @@ +/** + problem statement: + Given two strings- str1 and str2, check both are isomorphic or not + Isomorphic strings: Two strings are called isomorphic, + if there is a one to one mapping possible for every character of str1 to every character of str2 while preserving the order. + */ + + /*** + Example 1 => Input: str1="aab", str2="xxy" + Output: 1 + Example 2 => Input: str1="aab", str2="xyz" + Output: 0 + */ +/** + * + * Time Complexity: O(N) + Space Complexity: O(1) +*/ + + import java.util.Arrays; +import java.util.Scanner; + public class isomorphic_strings{ + public static void main(String[] args){ + Scanner input=new Scanner(System.in); + String str1=input.next(); + String str2=input.next(); + if(strings_are_isomorphic(str1,str2)){ + System.out.println("1"); + }else{ + System.out.println("0"); + } + } + public static boolean strings_are_isomorphic(String s1,String s2){ + int n=s1.length(); + int m=s2.length(); + //check length of both strings + if(n!=m){ + return false; + } + // array, make for mark visited characters of s2. + boolean visited[]=new boolean[256]; + //array, store mapping of every character from s1 to s2 + int map[]=new int[256]; + Arrays.fill(map,-1); + for (int i=0;i