博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode Isomorphic Strings
阅读量:6709 次
发布时间:2019-06-25

本文共 970 字,大约阅读时间需要 3 分钟。

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.

 

 

没做出来。

没有非常清楚的理解题目的意思,题目是说,两个字符串同构。我却认为是只要字符串s中哪些字符串一样,t中相同位置的字符串也要一样。

事实上约束更多,讲的是一种映射,就如同egg,e与a映射,g与d映射,那么字符串s中如果再出现e,字符串t中就必须是a。

所以,构建一个map,用上次见过的方法就行了。if(map.find(s[i])==map.end())****else****

不过需要有两次检查,检查s->t的映射t是否满足,还有t->s的映射s是否满足。

 

 

1 class Solution { 2 public: 3     bool isIsomorphic(string s, string t) { 4         map
model; 5 int slength=s.length(),tlength=t.length(); 6 if(slength!=tlength) return false; 7 for(int i=0;i

 

转载于:https://www.cnblogs.com/LUO77/p/5050356.html

你可能感兴趣的文章