here is the half code im making for isomorphic strings,
public boolean isIsomorphic(String s, String t) {
String[] ss=s.split("");
String[] tt=t.split("");
HashMap<String, Integer> smap=new HashMap<>();
HashMap<String, Integer> tmap=new HashMap<>();
int tint,sint;
for(int i=0;i<ss.length;i++){
if(!smap.containsKey(ss[i])){
smap.put(ss[i], 1);
}else{
sint=smap.get(ss[i]);
smap.put(ss[i], sint++);
}
}
for(int i=0;i<tt.length;i++){
if(!tmap.containsKey(tt[i])){
tmap.put(tt[i], 1);
}else{
tint=tmap.get(tt[i]);
tmap.put(tt[i], tint++);
}
}
//below space to iterate and check if both hashmap values are sequentially
}
below is the solution i found ,i was about to use this logic but i cant compare two hashmaps, as both have different keys... here all keys from both hashmaps are different, and values sequence wise might be same, thats all i wanna check...if all values are sequentially same or equal.
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
// ...
}
I am going to address the problem you asked: How might the contents of two
HashMapObjects be compered. This might or might not solve the problem of verifying twoStringObjects are isomorphic.Before addressing your question, I'd like to go off-topic:
I'd like to expand on a comment I made: Don't Repeat Yourself. In the code shown in your question, you have two sections that load a
Map, one section for eachStringparameter. You could reduce this to a method, and call it twice. Here is code similar to yours, but as a method:HashMap<Character, Integer>instead ofHashMap<String, Integer>.Characterinstead ofStringas keys, nor for using thecharAt (int index)method ofStringinstead of an array. It's my preference.StringAPI offers atoCharArray()method.toStringin theAbstractMapuseful.Now, to address the question:
You can use a
LinkedHashMap. A Linked Hash Map is a map that also has a doubly linked list. The linked list maintains the entries in insertion order.Although you want to iterate through the contents of the
MapObjects, theLinkedHashMapAPI doe not offer aget(int index)or other method that supports iteration using aforloop. Neither does it directly offer aniteratormethod.But, it does offer these methods:
public Set<Map.Entry<K,V>> entrySet()public Set<K> keySet()public Collection<V> values()Note that a
Setis a type ofCollection. And aCollectionisIterable.Collectionviews of theLinkedHashMap.Mapto anew Setornew CollectionObject.Mapas aSetorCollection.MapObjects.Now that you know those methods exist, and can find the API documentation, how you want to use them is up to you. Here is one way, but it is in keeping with using
Map<Character, Integer>:Note: I addressed only the question of sequentially comparing contents of a pair of
HashMaps. If my understanding of the Isomorphic Strings problem is correct, this code will produce false positives. That is, it may identify a pair ofStringsas isomorphic when they are not strictly isomorphic. I'll leave it to you to figure out why, to find an example of false positives.