## 一.題目描述
Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
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.
For example,?
Given “egg”, “add”, return true.
Given “foo”, “bar”, return false.
Given “paper”, “title”, return true.
Note:?
You may assume both s and t have the same length.
## 二.題目分析
這道題主要是建立兩個字符串中不同字符的對應關系,由于ASCII編碼的字符只有256個,而且string中是不保存null字符的,因此,可以建立兩個大小為256的字符數組來保存兩個string中每一個字符在另外一個string中的對應的關系,然后遍歷string中的每一個字符,如果相同位置的字符是互相對應的話,就處理下一個字符,如果不是互相對應的話,就在說明這兩個string不是同等結構的。
也可以通過兩個map來實現,但是map的查找過程時間復雜度為O(lgn),但是上面對于string中的每一個字符串都需要查找,因此,使用map的話,時間復雜度太高了。也可以使用hash表來做,也就是使用unordered_map來實現,但是由于ASCII編碼的字符的個數是固定的而且個數比較少,使用數組完全可以很好地實現。
## 三.示例代碼
~~~
class Solution
{
public:
bool isIsomorphic(string s, string t)
{
vector<unsigned char> First(256, 0); // 創建一個含有256個0拷貝的vector
vector<unsigned char> Second(256, 0);
for (size_t index = 0; index < s.size(); ++index)
{
unsigned char charOfFirst = s[index];
unsigned char charOfSecond = t[index];
unsigned char& First2Second = First[charOfFirst];
unsigned char& Second2First = Second[charOfSecond];
if (First2Second == 0 && Second2First == 0)
{
First2Second = charOfFirst;
Second2First = charOfSecond;
continue;
}
if (First2Second != 0 && Second2First != 0)
{
if (First2Second != charOfFirst && Second2First != charOfSecond)
return false;
continue;
}
return false;
}
return true;
}
};
~~~
示例結果:


## 四.小結
這道題就是尋找一個好的數據結構來保存兩個string之間的字符的對應關系,根據這道題的假設,選擇數組是一個比較的解決方案。
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016騰訊軟件開發筆試題)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契數列問題)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波蘭式的計算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和問題)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching