-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path205. Isomorphic Strings
35 lines (31 loc) · 1 KB
/
205. Isomorphic Strings
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution
{
public boolean isIsomorphic(String s, String t)
{
if (s. length() != t. length())
{
return false;
}
// Create a hashmap to store character mappings
Map<Character, Character> charMappingMap = new HashMap<>();
for (int i=0; i<s.length(); i++)
{
char original = s.charAt(i);
char replacement = t.charAt(i);
if (!charMappingMap.containsKey(original))
{
if (!charMappingMap.containsValue(replacement))
charMappingMap.put(original, replacement);
else
return false;
}
else
{
char mappedCharacter = charMappingMap.get(original);
if (mappedCharacter != replacement)
return false;
}
}
return true;
}
}