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. [systemd]Linux系统启动之systemd

    参照:https://wiki.debian.org/systemd 最近在添加板子应用程序自启动的时候,发现在rcN.d中的符号链接并没有用,文件系统为Debian Jessie 8, 后来从同事那 ...

  2. ZooKeepr日志清理(转)

    转载请用注明:@ni掌柜 nileader@gmail.com 在使用zookeeper过程中,我们知道,会有dataDir和dataLogDir两个目录,分别用于snapshot和事务日志的输出(默 ...

  3. elasticsearch -- 问题纪录

    报错1: [1]: max file descriptors [65535] for elasticsearch process is too low, increase to at least [6 ...

  4. Entity Framework(四):使用DbModelBuilder API创建表结构

    DbContext类有一个OnModelCreating方法,它用于流利地配置领域类到数据库模式的映射.下面我们以fluent API的方式来定义映射.首先,先将Product类注释掉,重新编写该类, ...

  5. 面向对象设计原则三:里氏替换原则(LSP)

    里氏替换原则(LSP)定义:在任何父类出现的地方都可以用它的子类类替换,且不影响功能.解释说明:其实LSP是对开闭原则的一个扩展,在OO思想中,我们知道对象是由一系列的状态和行为组成的,里氏替换原则说 ...

  6. Android学习笔记36:使用SQLite方式存储数据

    在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...

  7. 【BZOJ】1630: [Usaco2007 Demo]Ant Counting(裸dp/dp/生成函数)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1630 题意,给你n种数,数量为m个,求所有的数组成的集合选长度l-r的个数 后两者待会写.. 裸dp ...

  8. Elixir语言

    Elixir是一个基于Erlang VM的函数式元编程语言(类似Ruby),通过动态语言的灵活的语法和宏能够利用Erlang建立一个并发 分布 失败冗余的高质量代码

  9. c#方法生成mysql if方法(算工作日)

    public static string retunSQl(string s,string e){ return @"IF ( "+s+ ">" +e+ ...

  10. CStringArray序列化处理

    开发中需要对CStringArray进行保存操作,涉及到序列化,特总结一下: //写 CStringArray saTmp1; CStringArray saTmp2 saTmp1.AddString ...