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. Oracle复习(有记录才能沉淀.......)

    一.Oracle准备工作 1.安装Oracle Oracle数据库产品是免费的,我们可以从Oracle的官方网站(http://www.oracle.com)下载到程序安装包,Oracle在Windo ...

  2. 编译Spark2.1.2源码

    源码编译的shell脚本为 /dev/make-distribution.sh ,下载源码包解压就能找到.不同版本使用的参数有差异.可以直接查看make-distribution.sh文件. 下载sp ...

  3. &quot;crsctl check crs&quot; command hangs at EVMD check

     Pre-11gR2: "crsctl check crs" command hangs at EVMD check (文档 ID 1578875.1) APPLIES TO: ...

  4. 重启oracle方法一二三

    startup nomount alter database mount alter database open Linux:方法1 用root以ssh登录到linux,打开终端输入以下命令: cd ...

  5. Android多媒体系列2:利用MediaRecorder实现录音

  6. 2015 Multi-University Training Contest 3 1001 Magician

    Magician Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5316 Mean: n个数,2种操作,1是单点更新,2是询问区间 ...

  7. Unity基于DFGUI的TreeView设计

    using UnityEngine; using System.Collections; public class Item { public string Id; public string Nam ...

  8. QQ空间定时留言程序。

    已经可以自动登录了... 求指点..... 注意:启动时QQ号要填别人的.(留言程序只支持给别人留言) 源码路径:  https://github.com/gaoconggit/QQ-.git  

  9. jqgrid demo

    本人是用php写的,相信只要稍微用点时间看本人写的,就一定能看懂 前台代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//E ...

  10. php如何优化压缩的图片

    php程序开发中经常涉及到生成缩略图,利用php生成缩略图这个过程本身没难度,但是你知道php能够优化调节生成的缩略图的质量吗?也就是说php能够控制生成缩略图的清晰度以及生成后的缩略图的体积.下面我 ...