Add Isomorphic Strings (#3253)

Co-authored-by: Andrii Siriak <siryaka@gmail.com>
This commit is contained in:
Mann
2022-09-13 04:59:26 +05:30
committed by GitHub
parent 1e4c4a112d
commit 20a1f40c5a
2 changed files with 63 additions and 0 deletions

View File

@ -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;
}
}