# Easy-题目46：205. Isomorphic Strings

66人阅读 评论(0)

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 “foo”, “bar”, return false.
Given “paper”, “title”, return true.
Note:
You may assume both s and t have the same length..

public class Solution {
public boolean isIsomorphic(String s, String t) {
int slen=s.length();
int tlen=t.length();
if(slen!=tlen)
return false;
else
{
HashMap<Character,Character> map=new HashMap();
for(int i=0;i<slen;i++)
{
if(!map.containsKey(s.charAt(i)))
map.put(s.charAt(i),t.charAt(i));
else
{
if(map.get(s.charAt(i)) != t.charAt(i))
return false;
}
}
map.clear();
for(int i=0;i<slen;i++)
{
if(!map.containsKey(t.charAt(i)))
map.put(t.charAt(i),s.charAt(i));
else
{
if(map.get(t.charAt(i)) != s.charAt(i))
return false;
}
}
return true;
}
}
}

34ms,beats 30.09%,众数26ms，8.93%

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：63231次
• 积分：3275
• 等级：
• 排名：第10713名
• 原创：270篇
• 转载：53篇
• 译文：0篇
• 评论：8条
文章分类
阅读排行
最新评论