Repository: TheAlgorithms/Java
Pull request #3253 (2022-09-06 16:06:22)

Added Isomorphic strings with the changes
### **Describe your change:**

- [x] Add an algorithm?
- [ ] Fix a bug or typo in an existing algorithm?
- [ ] Documentation change?

#### References

<!-- Add any reference to previous pull-request or issue -->

### **Checklist:**

<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [x] All functions and variable names follow Java naming conventions.
- [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #3236 `.


Mann-tech13: All changes updated in current pull request.
Mann-tech13: Pl. check once


Patch:
diff --git a/src/main/java/com/thealgorithms/strings/Isomorphic.java b/src/main/java/com/thealgorithms/strings/Isomorphic.java
new file mode 100644
index 00000000..661aee46
--- /dev/null
+++ b/src/main/java/com/thealgorithms/strings/Isomorphic.java
@@ -0,0 +1,33 @@
+package com.thealgorithms.strings;
+import java.util.*;
+
+public class Isomorphic {
+  public static boolean checkStrings(String s, String t) {
+    if(s.length() != t.length()){
+        return false;
+    }
+
+    // To mark the characters of string using MAP
+    // character of first string as KEY and another as VALUE
+    // now check occurence by keeping the track with SET data structure
+    Map<Character, Character> characterMap = new HashMap<Character, Character>();
+    Set<Character> trackUinqueCharacter = new HashSet<Character>();
+    
+    for(int i=0; i<s.length(); i++){
+        if(characterMap.containsKey(s.charAt(i))){
+            if(t.charAt(i) != characterMap.get(s.charAt(i))){
+                return false;
+            }
+        }
+        else{
+            if(trackUinqueCharacter.contains(t.charAt(i))){
+                return false;
+            }
+            
+            characterMap.put(s.charAt(i), t.charAt(i));
+        }
+        trackUinqueCharacter.add(t.charAt(i));
+    }
+    return true;
+  }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/strings/IsomorphicTest.java b/src/test/java/com/thealgorithms/strings/IsomorphicTest.java
new file mode 100644
index 00000000..1a83e607
--- /dev/null
+++ b/src/test/java/com/thealgorithms/strings/IsomorphicTest.java
@@ -0,0 +1,30 @@
+package com.thealgorithms.strings;
+import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.*;
+import java.util.*;
+
+public class IsomorphicTest {
+
+    @Test
+    public static void main(String[] args) {
+
+    String str1 = "abbbbaac";
+    String str2 = "kffffkkd";
+
+    String str3 = "xyxyxy";
+    String str4 = "bnbnbn";
+
+    String str5 = "ghjknnmm";
+    String str6 = "wertpopo";
+
+    String str7 = "aaammmnnn";
+    String str8 = "ggghhhbbj";
+
+    Isomorphic isomorphic = new Isomorphic();
+
+    assertTrue(isomorphic.checkStrings(str1, str2));
+    assertTrue(isomorphic.checkStrings(str3, str4));
+    assertFalse(isomorphic.checkStrings(str5, str6));
+    assertFalse(isomorphic.checkStrings(str7, str8));
+  }
+}