Skip to content

Commit d37ed46

Browse files
committed
leetcode
1 parent a8c44ed commit d37ed46

File tree

4 files changed

+235
-0
lines changed

4 files changed

+235
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
3+
-* Isomorphic Strings *-
4+
5+
Given two strings s and t, determine if they are isomorphic.
6+
7+
Two strings s and t are isomorphic if the characters in s can be replaced to get t.
8+
9+
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
10+
11+
12+
13+
Example 1:
14+
15+
Input: s = "egg", t = "add"
16+
Output: true
17+
Example 2:
18+
19+
Input: s = "foo", t = "bar"
20+
Output: false
21+
Example 3:
22+
23+
Input: s = "paper", t = "title"
24+
Output: true
25+
26+
27+
Constraints:
28+
29+
1 <= s.length <= 5 * 104
30+
t.length == s.length
31+
s and t consist of any valid ascii character.
32+
33+
34+
*/
35+
36+
// class A {
37+
// bool isIsomorphic(String s, String t) {
38+
// Map<String, int>? table = {}, tTable = {};
39+
// for (int i = 0; i < s.length; i++) {
40+
// // 15 is magic number
41+
// int bit = (1 << (i % 15));
42+
43+
// if( table[s[i]] != null){
44+
// table[s[i]] |= bit;
45+
// }
46+
47+
// tTable[t[i]] |= bit;
48+
49+
// if (table[s[i]] != tTable[t[i]]) {
50+
// return false;
51+
// }
52+
// }
53+
54+
// return true;
55+
// }
56+
// }
57+
58+
class B {
59+
// Runtime: 625 ms, faster than 16.13% of Dart online submissions for Isomorphic Strings.
60+
// Memory Usage: 140.6 MB, less than 100.00% of Dart online submissions for Isomorphic Strings.
61+
bool isIsomorphic(String s, String t) {
62+
// Base case: for different length of two strings...
63+
if (s.length != t.length) return false;
64+
// Create two maps for s & t strings...
65+
List<int> map1 = List.filled(256, 0);
66+
List<int> map2 = List.filled(256, 0);
67+
// Traverse all elements through the loop...
68+
for (int idx = 0; idx < s.length; idx++) {
69+
// Compare the maps, if not equal, return false...
70+
if (map1[s.codeUnitAt(idx)] != map2[t.codeUnitAt(idx)]) return false;
71+
// Insert each character if string s and t into seperate map...
72+
map1[s.codeUnitAt(idx)] = idx + 1;
73+
map2[t.codeUnitAt(idx)] = idx + 1;
74+
}
75+
return true; // Otherwise return true...
76+
}
77+
}
78+
79+
class C {
80+
// Runtime: 611 ms, faster than 16.13% of Dart online submissions for Isomorphic Strings.
81+
// Memory Usage: 144.2 MB, less than 64.52% of Dart online submissions for Isomorphic Strings.
82+
bool isIsomorphic(String s, String t) {
83+
Map mapS = {}, mapT = {};
84+
for (int i = 0; i < s.length; i++) {
85+
if (mapS[s[i]] != null && mapT[t[i]] != null) {
86+
mapS[s[i]] = t[i];
87+
mapT[t[i]] = s[i];
88+
} else if (mapS[s[i]] != t[i] || mapT[t[i]] != s[i]) return false;
89+
}
90+
return true;
91+
}
92+
}
93+
94+
class D {
95+
bool isIsomorphic(String s, String t) {
96+
Map cypher = Map();
97+
for (int i = 0; i < s.length; i++) {
98+
if (cypher.containsKey(s[i])) {
99+
//cypher.set(s[i], t[i]);
100+
cypher.forEach((key, value) {
101+
key = s[i];
102+
value = t[i];
103+
});
104+
} else if (cypher.entries.contains(s.codeUnitAt(i)) != t.codeUnitAt(i)) {
105+
return false;
106+
}
107+
}
108+
return cypher.values.length == cypher.length;
109+
}
110+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
func isIsomorphic(s string, t string) bool {
4+
if len(s) != len(t) {
5+
return false
6+
}
7+
8+
m := make(map[byte]byte)
9+
mt := make(map[byte]byte)
10+
11+
for i := 0; i < len(s); i++ {
12+
t1 := t[i]
13+
14+
if value, ok := m[t1]; ok {
15+
if value != s[i] {
16+
return false
17+
}
18+
} else {
19+
m[t1] = s[i]
20+
}
21+
22+
s1 := s[i]
23+
24+
if value, ok := mt[s1]; ok {
25+
if value != t[i] {
26+
return false
27+
}
28+
} else {
29+
mt[s1] = t[i]
30+
}
31+
}
32+
33+
return true
34+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# 🔥 Isomorphic Strings 🔥 || 3 Solutions || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1
4+
5+
```dart
6+
class Solution {
7+
// Runtime: 625 ms, faster than 16.13% of Dart online submissions for Isomorphic Strings.
8+
// Memory Usage: 140.6 MB, less than 100.00% of Dart online submissions for Isomorphic Strings.
9+
bool isIsomorphic(String s, String t) {
10+
// Base case: for different length of two strings...
11+
if (s.length != t.length) return false;
12+
// Create two maps for s & t strings...
13+
List<int> map1 = List.filled(256, 0);
14+
List<int> map2 = List.filled(256, 0);
15+
// Traverse all elements through the loop...
16+
for (int idx = 0; idx < s.length; idx++) {
17+
// Compare the maps, if not equal, return false...
18+
if (map1[s.codeUnitAt(idx)] != map2[t.codeUnitAt(idx)]) return false;
19+
// Insert each character if string s and t into separate map...
20+
map1[s.codeUnitAt(idx)] = idx + 1;
21+
map2[t.codeUnitAt(idx)] = idx + 1;
22+
}
23+
return true; // Otherwise return true...
24+
}
25+
}
26+
```
27+
28+
## Solution - 2
29+
30+
```dart
31+
class C {
32+
// Runtime: 611 ms, faster than 16.13% of Dart online submissions for Isomorphic Strings.
33+
// Memory Usage: 144.2 MB, less than 64.52% of Dart online submissions for Isomorphic Strings.
34+
bool isIsomorphic(String s, String t) {
35+
// Second map to hold the value and key of second String (t)
36+
// First map to hold the value and key of first String (s)
37+
Map mapS = {}, mapT = {};
38+
//iterating through each element of the string
39+
for (int i = 0; i < s.length; i++) {
40+
// if they both are not null means they have values
41+
if (mapS[s[i]] != null && mapT[t[i]] != null) {
42+
// so the first map will hold the each string element individually fo every element
43+
// base on key and value
44+
mapS[s[i]] = t[i];
45+
mapT[t[i]] = s[i];
46+
// this cause if their value is not same as the element of the each string
47+
} else if (mapS[s[i]] != t[i] || mapT[t[i]] != s[i]) return false;
48+
}
49+
return true;
50+
}
51+
}
52+
```
53+
54+
## Solution - 3
55+
56+
```go
57+
func isIsomorphic(s string, t string) bool {
58+
if len(s) != len(t) {
59+
return false
60+
}
61+
62+
m := make(map[byte]byte)
63+
mt := make(map[byte]byte)
64+
65+
for i := 0; i < len(s); i++ {
66+
t1 := t[i]
67+
68+
if value, ok := m[t1]; ok {
69+
if value != s[i] {
70+
return false
71+
}
72+
} else {
73+
m[t1] = s[i]
74+
}
75+
76+
s1 := s[i]
77+
78+
if value, ok := mt[s1]; ok {
79+
if value != t[i] {
80+
return false
81+
}
82+
} else {
83+
mt[s1] = t[i]
84+
}
85+
}
86+
87+
return true
88+
}
89+
90+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
7171
- [Number of Dice Rolls With Target Sum](NumberOfDiceRollsWithTargetSum/number_of_dice_rolls_with_target_sum.dart)
7272
- [Remove Linked List Elements](RemoveLinkedListElements/remove_linked_list_elements.dart)
7373
- [Minimum Time to Make Rope Colorful](MinimumTimeToMakeRopeColorful/minimum_time_to_make_rope_colorful.dart)
74+
- [Isomorphic Strings](IsomorphicStrings/isomorphic_strings.dart)
7475

7576
## Reach me via
7677

0 commit comments

Comments
 (0)