You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
🔥 Isomorphic Strings 🔥 || 3 Solutions || Simple Fast and Easy || with Explanation
Solution - 1
classSolution {
// Runtime: 625 ms, faster than 16.13% of Dart online submissions for Isomorphic Strings.// Memory Usage: 140.6 MB, less than 100.00% of Dart online submissions for Isomorphic Strings.boolisIsomorphic(String s, String t) {
// Base case: for different length of two strings...if (s.length != t.length) returnfalse;
// Create two maps for s & t strings...List<int> map1 =List.filled(256, 0);
List<int> map2 =List.filled(256, 0);
// Traverse all elements through the loop...for (int idx =0; idx < s.length; idx++) {
// Compare the maps, if not equal, return false...if (map1[s.codeUnitAt(idx)] != map2[t.codeUnitAt(idx)]) returnfalse;
// Insert each character if string s and t into separate map...
map1[s.codeUnitAt(idx)] = idx +1;
map2[t.codeUnitAt(idx)] = idx +1;
}
returntrue; // Otherwise return true...
}
}
Solution - 2
classC {
// Runtime: 611 ms, faster than 16.13% of Dart online submissions for Isomorphic Strings.// Memory Usage: 144.2 MB, less than 64.52% of Dart online submissions for Isomorphic Strings.boolisIsomorphic(String s, String t) {
// Second map to hold the value and key of second String (t)// First map to hold the value and key of first String (s)Map mapS = {}, mapT = {};
//iterating through each element of the stringfor (int i =0; i < s.length; i++) {
// if they both are not null means they have valuesif (mapS[s[i]] !=null&& mapT[t[i]] !=null) {
// so the first map will hold the each string element individually fo every element// base on key and value
mapS[s[i]] = t[i];
mapT[t[i]] = s[i];
// this cause if their value is not same as the element of the each string
} elseif (mapS[s[i]] != t[i] || mapT[t[i]] != s[i]) returnfalse;
}
returntrue;
}
}