205. Isomorphic Strings 字符串相似
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),民勤企業(yè)網(wǎng)站建設(shè),民勤品牌網(wǎng)站建設(shè),網(wǎng)站定制,民勤網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,民勤網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
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.
題目大意:
判斷兩個(gè)字符串是否相似。
思路:
使用雙map來進(jìn)行比較。map鍵為字符串元素,值為字符上一次出現(xiàn)的位置。
代碼如下:
class Solution { public: bool isIsomorphic(string s, string t) { if(s.size() != t.size()) return false; unordered_mapmaps; unordered_map mapt; for(int i = 0;i < s.size();i++) { if(maps.find(s[i]) == maps.end() && mapt.find(t[i]) == mapt.end()) { maps.insert(pair (s[i],i)); mapt.insert(pair (t[i],i)); } else if(maps.find(s[i]) != maps.end() && mapt.find(t[i]) != mapt.end()) { if(maps[s[i]] != mapt[t[i]]) return false; else { maps[s[i]] = i; mapt[t[i]] = i; } } else return false; } return true; } };
2016-08-13 17:15:21