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.
题解:显然是哈希查找,但是其实没必要用map,一共就255个字符,每次只记录它现在的位置就好(因为之前的位置都比较过了)。
class Solution { public: bool isIsomorphic(string s, string t) { int hs[256]={0}; int ht[256]={0}; int ls=s.length(),lt=t.length(); if(ls!=lt) return false; for(int i=0;i<ls;i++){ if(hs[s[i]]!=ht[t[i]]){ return false; } hs[s[i]]=i+1; ht[t[i]]=i+1; } return true; } };