LeetCode_Isomorphic Strings
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的更多相关文章
- leetcode_Isomorphic Strings _easy
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Hacker Rank: Two Strings - thinking in C# 15+ ways
March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...
- 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 ...
- Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- [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 ...
- [LeetCode] Group Shifted Strings 群组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- [LeetCode] Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- [LeetCode] Multiply Strings 字符串相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
随机推荐
- arrow:让Python的日期与时间变的更好
在处理数据的时候经常会碰见各种时间数据,但因为时间数据的格式不统一,所以导致数据处理的时候有一些麻烦.Python的标准库提供了相应模块,但可用性却不高,也不够人性化.本专栏之前已经有文章介绍过在R中 ...
- 《TCP/IP图解》读书笔记
看这本书的目的: 了解计算机之间是怎么通信的 熟悉TCP/IP协议 后面就这两个目的进行展开,要达到这两个目的,读这本书,学到了哪些知识. 一.计算机之间是怎么通信的 先来了解下面几个概念,中继器,二 ...
- 示例 - C#脚本代码采集搜狐NBA球员, 球队和比赛实况
最近 @甜瓜 (QQ:1069629945) 开发了一套NBA数据采集脚本, 我觉得很赞. 经他允许发布出来和大家分享一些经验: 球员球队: http://data.sports.sohu.com/n ...
- dp - HNU 13404 The Imp
The Imp Problem's Link: http://acm.hnu.cn/online/?action=problem&type=show&id=13404&cour ...
- MapReduce 中的两表 join 几种方案简介
转自:http://my.oschina.net/leejun2005/blog/95186 MapSideJoin例子:http://my.oschina.net/leejun2005/blog/1 ...
- HBase源码学习系列
转自:http://www.cnblogs.com/cenyuhai/tag/hbase%E6%BA%90%E7%A0%81%E7%B3%BB%E5%88%97/ (mark) hbase源码系列(十 ...
- php ut8声明
header("Content-type: text/html; charset=utf-8");
- thinkphp 使用外部php或html 原理
- Differential Geometry之第十章极小曲面
第十章.极小曲面 1.极小图 Animation showing the deformation of a helicoid into a catenoid. Animation of Scherk' ...
- 字符串类为JAVA中的特殊类
字符串类为JAVA中的特殊类,String中为final类,一个字符串的值不可重复.因此在JAVA VM(虚拟机)中有一个字符串池,专门用来存储字符串.如果遇到String a=”hello”时(注意 ...