This is one of those problems where the clean idea matters more than the amount of code. For 205. Isomorphic Strings, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are isIsomorphic, ms.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- A set is doing the membership or uniqueness work, which keeps the main loop readable.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 24 ms, faster than 8.57% of C++ online submissions for Isomorphic Strings.
02//Memory Usage: 9.3 MB, less than 7.37% of C++ online submissions for Isomorphic Strings.
03
04class Solution {
05public:
06 bool isIsomorphic(string s, string t) {
07 if(s.size() != t.size()) return false;
08
09 //need to ensure 1-to-1 mapping,
10 //so we need to maps
11 map<char, char> m, im;
12 set<char> values;
13 for(int i = 0; i < s.size(); i++){
14 if(m.find(s[i]) == m.end()){
15 m[s[i]] = t[i];
16 }else if(m[s[i]] != t[i]){
17 return false;
18 }
19 if(im.find(t[i]) == im.end()){
20 im[t[i]] = s[i];
21 }else if(im[t[i]] != s[i]){
22 return false;
23 }
24 }
25
26 return true;
27 }
28};
29
30//https://leetcode.com/problems/isomorphic-strings/discuss/57796/My-6-lines-solution
31/**
32indirect mapping
33**/
34
35//Runtime: 16 ms, faster than 36.56% of C++ online submissions for Isomorphic Strings.
36//Memory Usage: 9 MB, less than 32.11% of C++ online submissions for Isomorphic Strings.
37
38class Solution {
39public:
40 bool isIsomorphic(string s, string t) {
41 vector<int> ms(128, 0), mt(128, 0);
42
43 for(int i = 0; i < s.size(); i++){
44 if(ms[s[i]] != mt[t[i]]){
45 return false;
46 }
47 ms[s[i]] = i+1;
48 mt[t[i]] = i+1;
49 }
50
51 return true;
52 }
53};
Cost