Skip to content

Commit 68e137d

Browse files
committed
Add isomorphic strings
1 parent 9fd09d6 commit 68e137d

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ List of Programs related to data structures and algorithms
206206
| 8 | Letter combinations | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/8.letterCombinations/letterCombinations.js) | [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/8.letterCombinations/letterCombinations.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/8.letterCombinations/letterCombinations.md) | Medium | Backtracking with hash table mapping |
207207
| 9 | LRU Cache | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/9.lruCache/lruCache.js) | [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/9.lruCache/lruCache.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/9.lruCache/lruCache.md) | Medium | Combination of Hash Table and doubly linked list |
208208
| 10 | Maximum number of balloons | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/10.maximumBalloons/maximumBalloons.js) | [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/10.maximumBalloons/maximumBalloons.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/10.maximumBalloons/maximumBalloons.md) | Easy | Hash map on characters |
209+
| 11 | Isomorphic Strings | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/11.isomorphicStrings/isomorphicStrings.js) | [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/11.isomorphicStrings/isomorphicStrings.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/11.isomorphicStrings/isomorphicStrings.md) | Easy | mapping characters using Hash maps |
209210

210211
## Sorting
211212

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package java1.algorithms.hashmap.isomorphicStrings;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class IsomorphicStrings {
7+
private static boolean isIsomorphic(String s, String t){
8+
if(s.length() != t.length()) {
9+
return false;
10+
}
11+
12+
Map<Character, Character> sMap = new HashMap<>();
13+
Map<Character, Character> tMap = new HashMap<>();
14+
15+
for(int i=0; i<s.length(); i++) {
16+
char charS = s.charAt(i);
17+
char charT = t.charAt(i);
18+
if(sMap.containsKey(charS)) {
19+
if(sMap.get(charS) != charT) {
20+
return false;
21+
}
22+
} else {
23+
sMap.put(charS, charT);
24+
}
25+
26+
if(tMap.containsKey(charT)) {
27+
if(tMap.get(charT) != charS) {
28+
return false;
29+
}
30+
} else {
31+
tMap.put(charT, charS);
32+
}
33+
}
34+
35+
return true;
36+
}
37+
38+
public static void main(String[] args) {
39+
String s1 = "dad", t1 = "mom";
40+
String s2 = "zoo", t2 = "cat";
41+
System.out.println(isIsomorphic(s1, t1));
42+
System.out.println(isIsomorphic(s2, t2));
43+
}
44+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
**Problem statement:**
2+
Given two strings `s` and `t`, determine if they are isomorphic or not.
3+
4+
**Note:** Two strings can be called as isomorphic if the characters in one word can be replaced to get another word. You need to make sure that all occurrences of a character must be replaced with another character while preserving the order of characters. Also, no two unique characters map to the same character, but a character map to itself.
5+
6+
## Examples:
7+
Example 1:
8+
9+
Input: s = "dad", t="mom"
10+
11+
Output: true
12+
13+
14+
Example 2:
15+
16+
Input: s = "zoo", t="cat"
17+
18+
Output: false
19+
20+
21+
**Algorithmic Steps**
22+
This problem is solved with the help of maps to verify one-to-one mapping between every character of the first string to the second string.
23+
24+
1. Add a base case check by returning false when two strings lengths are not equal.
25+
26+
2. Define two hash maps(i.e, `sMap`, `tMap`) to track mappings between two strings.
27+
28+
3. Iterate over characters of given string(either string `s` or `t`).
29+
1. If the current character of first string already exists in `sMap`, and if the current character in the first string has been mapped to a conflict character in the second string, return `false`indicating that given strings are not isomorphic. Otherwise(when there is no conflict mapping), add the character mapping in `sMap`.
30+
31+
2. If the current character of second string already exists in `tMap`, and if the current character in the second string has been mapped to a conflict character in the first string, return `false`indicating that given strings are not isomorphic. Otherwise(when there is no conflict mapping), add the character mapping in `tMap`.
32+
33+
4. If the above iteration is complete without any conflicts, those two strings are considered as isomorphic. i.e, Return `true` indicating the given strings as isomorphic strings.
34+
35+
**Time and Space complexity:**
36+
This algorithm has a time complexity of `O(n)`, Where `n` is the length of the input strings. This is because we traverse the string once and perform constant time operations for hash map lookups and insertions.
37+
38+
Here, it takes time complexity of `O(n)`. This is because of the space required by two hash maps(i.e, `sMap`,`tMap`).
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function isIsomorphic(s, t) {
2+
if(s.length !== t.length){
3+
return false;
4+
}
5+
let sMap = new Map();
6+
let tMap = new Map();
7+
8+
for (let i = 0; i < s.length; i++) {
9+
if(sMap.has(s[i])) {
10+
if(t[i] !== sMap.get(s[i])) {
11+
return false;
12+
}
13+
} else {
14+
sMap.set(s[i], t[i]);
15+
}
16+
if(tMap.has(t[i])) {
17+
if(s[i] !== tMap.get(t[i])) {
18+
return false;
19+
}
20+
} else {
21+
tMap.set(t[i], s[i]);
22+
}
23+
}
24+
return true;
25+
}
26+
27+
const s1 = "dad", t1 = "mom";
28+
const s2 = "zoo", t2 = "cat";
29+
console.log(isIsomorphic(s1, t1));
30+
console.log(isIsomorphic(s2, t2));
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
**Problem statement:**
2+
Given two strings `s` and `t`, determine if they are isomorphic or not.
3+
4+
**Note:** Two strings can be called as isomorphic if the characters in one word can be replaced to get another word. You need to make sure that all occurrences of a character must be replaced with another character while preserving the order of characters. Also, no two unique characters map to the same character, but a character map to itself.
5+
6+
## Examples:
7+
Example 1:
8+
9+
Input: s = "dad", t="mom"
10+
11+
Output: true
12+
13+
14+
Example 2:
15+
16+
Input: s = "zoo", t="cat"
17+
18+
Output: false
19+
20+
21+
**Algorithmic Steps**
22+
This problem is solved with the help of maps to verify one-to-one mapping between every character of the first string to the second string.
23+
24+
1. Add a base case check by returning false when two strings lengths are not equal.
25+
26+
2. Define two hash maps(i.e, `sMap`, `tMap`) to track mappings between two strings.
27+
28+
3. Iterate over characters of given string(either string `s` or `t`).
29+
1. If the current character of first string already exists in `sMap`, and if the current character in the first string has been mapped to a conflict character in the second string, return `false`indicating that given strings are not isomorphic. Otherwise(when there is no conflict mapping), add the character mapping in `sMap`.
30+
31+
2. If the current character of second string already exists in `tMap`, and if the current character in the second string has been mapped to a conflict character in the first string, return `false`indicating that given strings are not isomorphic. Otherwise(when there is no conflict mapping), add the character mapping in `tMap`.
32+
33+
4. If the above iteration is complete without any conflicts, those two strings are considered as isomorphic. i.e, Return `true` indicating the given strings as isomorphic strings.
34+
35+
**Time and Space complexity:**
36+
This algorithm has a time complexity of `O(n)`, Where `n` is the length of the input strings. This is because we traverse the string once and perform constant time operations for hash map lookups and insertions.
37+
38+
Here, it takes time complexity of `O(n)`. This is because of the space required by two hash maps(i.e, `sMap`,`tMap`).

0 commit comments

Comments
 (0)