Isomorphic Strings 解答
Question
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.
Solution 1
Use hashmap, remember to check both key and value. Time complexity O(n^2), space cost O(n).
hashMap.containsValue costs O(n)
public class Solution {
public boolean isIsomorphic(String s, String t) {
if (s == null || t == null)
return false;
if (s.length() != t.length())
return false;
if(s.length()==0 && t.length()==0)
return true;
int length = s.length();
Map<Character, Character> map = new HashMap<Character, Character>();
for (int i = 0; i < length; i++) {
char tmp1 = s.charAt(i);
char tmp2 = t.charAt(i);
if (map.containsKey(tmp1)) {
if (map.get(tmp1) != tmp2)
return false;
} else if (map.containsValue(tmp2)) {
return false;
} else {
map.put(tmp1, tmp2);
}
}
return true;
}
}
Solution 2
Use extra space to reduce time complexity.
public class Solution {
public boolean isIsomorphic(String s, String t) {
if (s == null || t == null)
return false;
if (s.length() != t.length())
return false;
if(s.length()==0 && t.length()==0)
return true;
int length = s.length();
Map<Character, Character> map = new HashMap<Character, Character>();
Set<Character> counts = new HashSet<Character>();
for (int i = 0; i < length; i++) {
char tmp1 = s.charAt(i);
char tmp2 = t.charAt(i);
if (map.containsKey(tmp1)) {
if (map.get(tmp1) != tmp2)
return false;
} else if (counts.contains(tmp2)) {
return false;
} else {
map.put(tmp1, tmp2);
counts.add(tmp2);
}
}
return true;
}
}
Isomorphic Strings 解答的更多相关文章
- [LeetCode] Isomorphic Strings
Isomorphic Strings Total Accepted: 30898 Total Submissions: 120944 Difficulty: Easy Given two string ...
- leetcode:Isomorphic Strings
Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are isom ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- [leetcode]205. Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Codeforces 985 F - Isomorphic Strings
F - Isomorphic Strings 思路:字符串hash 对于每一个字母单独hash 对于一段区间,求出每个字母的hash值,然后排序,如果能匹配上,就说明在这段区间存在字母间的一一映射 代 ...
- Educational Codeforces Round 44 (Rated for Div. 2) F - Isomorphic Strings
F - Isomorphic Strings 题目大意:给你一个长度为n 由小写字母组成的字符串,有m个询问, 每个询问给你两个区间, 问你xi,yi能不能形成映射关系. 思路:这个题意好难懂啊... ...
- CodeForces985F -- Isomorphic Strings
F. Isomorphic Strings time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
- LeetCode 205. 同构字符串(Isomorphic Strings)
205. 同构字符串 205. Isomorphic Strings
- LeetCode_205. Isomorphic Strings
205. Isomorphic Strings Easy Given two strings s and t, determine if they are isomorphic. Two string ...
随机推荐
- c++ 05
一.单例模式 二.成员指针 class Student { public: string m_name; void print (void) { ... } }; 1.指向成员变量的指针 成员 ...
- hdu3870-Catch the Theves(平面图最小割)
Problem Description A group of thieves is approaching a museum in the country of zjsxzy,now they are ...
- VMware+Ubuntu8.10+Skyeye+gdb实现u-boot源码调试
系统平台:WindowsXP 虚拟机: VMware Workstation 6.5.0 Ubuntu8.10 安装程序 ubuntu-8.10-desktop-i386.iso 下载地址:http: ...
- Unity 代码检测单击,双击,拖放
今天小伙伴问我如何自己写一段代码检测 单击 双击 和 拖放.于是就写了这段代码O(∩_∩)O~ 代码如下: using UnityEngine; using System.Collections; p ...
- jquery.validate详解二
五.常用方法及注意问题 1.用其他方式替代默认的SUBMIT $().ready(function() { $("#signupForm").validate({ ...
- hdoj 3400 三分
两次三分 #include <iostream> #include <cstdio> #include <cstring> #include <cmath&g ...
- Myeclipse安装破解
- CI框架源代码阅读笔记3 全局函数Common.php
从本篇開始.将深入CI框架的内部.一步步去探索这个框架的实现.结构和设计. Common.php文件定义了一系列的全局函数(一般来说.全局函数具有最高的载入优先权.因此大多数的框架中BootStrap ...
- poj1562--Oil Deposits
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- DBSNMP和SYSMAN用户初始密码及正确的修改方式
SYSMAN和DBSNMP跟涉及到Oracle的EM,所以跟其他的用户修改密码方式有所区别. 下面是这两个用户的默认密码和作用说明 DBSNMP DBSNMP The account used by ...