Isomorphic Strings

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.

判断两个字符串是否同构:s中的每个字符按顺序映射到t中一个字符,映射关系不能是一对多,例如"foo"和"bar",按顺序映射:f->b,o->a,o->r,o同时对a和r,所以foo和egg不是同构的;同理,不能多对一,即s中不同字符不能对应t中相同的字符,例如,"ab"和"aa",a->a,b->a,a和b同时对应a,所以ab和aa不符合条件。

以s中的每个字符作为key,t中的每个字符作为value,利用Java中的map容器做映射。

public class Solution {
public boolean isIsomorphic(String s, String t) {
Map<Character,Character> mp1 = new HashMap<>();
final int len1 = s.length();
final int len2 = s.length();
if(len1!=len2) return false;
if(len1==0) return true;
for(int i = 0;i < len1;i++)
{
if(!mp1.containsKey(s.charAt(i)))
{
mp1.put(s.charAt(i),t.charAt(i));
for(int j = 0;j<i;j++)
{
if(mp1.get(s.charAt(i))==mp1.get(s.charAt(j)))//多对一
{
return false;
}
}
}
else
{
if(mp1.get(s.charAt(i))!=t.charAt(i))//一对多
{
return false;
}
}
}
return true;
}
}

时间复杂度较高,期待更高效的方法。

LeetCode_Isomorphic Strings的更多相关文章

  1. leetcode_Isomorphic Strings _easy

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  2. Hacker Rank: Two Strings - thinking in C# 15+ ways

    March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...

  3. StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?

    StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...

  4. Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  5. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  6. [LeetCode] Encode and Decode Strings 加码解码字符串

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  7. [LeetCode] Group Shifted Strings 群组偏移字符串

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  8. [LeetCode] Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  9. [LeetCode] Multiply Strings 字符串相乘

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

随机推荐

  1. ibatis时间比较大小

     <![CDATA[          A.RFID_Time >= #StartTime#          ]]>时间搜索功能 A.RFID_Time <![CDATA[  ...

  2. java strtus2 拦截器(Interceptors)

    在strtus2 中有一个比较重要的东西就是拦截器(Interceptors) 拦截器可以做到在已有的业务中插入一块共通的,比如在一个业务中,直接插入一串登录功能,就不用去每个页面一个个去显示是否登录 ...

  3. Excel关闭事件

    记录一下,弄VBA曾经遇到一个需求,遇到用到这个事件,找了很久,最后还是问别人才知道的. Sub Auto_Close() ThisWorkbook.Saved = True End Sub

  4. Eclipse4.4 安装java反编译插件Eclipse Class Decompiler

    一.在线安装方式: Eclipse Class Decompiler整合了眼下最好的2个Java反编译工具Jad和JD-Core,而且和Eclipse Class Viewer无缝集成.可以非常方便的 ...

  5. Keil(MDK-ARM)系列教程(五)_Configuration(Ⅰ)

    推荐 分享一个大神的人工智能教程.零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到人工智能的队伍中来! http://www.captainbed.net/strongerhuang Ⅰ.写在前 ...

  6. 框架Iframe的退出,IE 火狐都没问题 到360就不跳转页面 刷新一遍才跳转到登录页

    遇到这种情况 ,郁闷死了,来回折腾好几种跳转方法,最后有一个灵感,当我点击退出按钮的时候,我是用jquery $("#ID").click(function(){}) 这种方法异步 ...

  7. 在linux 列出 超级用户 普通用户和 系统用户

    #!/bin/bash echo Please select which list you want to print echo "1. admin (enter 1)" echo ...

  8. Bitcoin: A Peer-to-Peer Electronic Cash System(比特币论文翻译)

    比特币历史: 2008年,比特币论文诞生 2009年1月,第一批比特币诞生 2011年4月,比特币价格第一次达到了1美元 2011年6月,涨到30美元,然后开始跌 2013年1月,4美元 2013年1 ...

  9. 32Mybatis_mybatis逆向工程自动生成代码

    实际工作中要用的.很重要! mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml ...

  10. MyBatis的优缺点

    优点: 1.简单易学 mybatis本身就很小且简单.没有任何第三方依赖,最简单安装只要两个jar文件+配置几个sql映射文件易于学习,易于使用,通过文档和源代码,可以比较完全的掌握它的设计思路和实现 ...