LeetCode(44)- 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.
思路:
- 题意:给定两个同样长度的字符串,判断这两个字符串是不是同形,所谓同形就是,两个字符串相等的地方一样,如上面举例的字符串。
- 可以利用判断字符串是不是有重复字符的思路,把字符串转化为数组,存进hashMap,判断是否有重复值,有了判断相应的坐标是不是相同,不相同,或者不同时出现重复,均为不满足条件。出现重复,去掉重复,添加新元素,不重复,只是添加新元素。
-
代码:
public class Solution {
public boolean isIsomorphic(String s, String t) {
if(s == null){
return true;
}
char[] a = s.toCharArray();
char[] b = t.toCharArray();
int n = a.length;
Map<Character,Integer> aa = new HashMap<Character,Integer>();
Map<Character,Integer> bb = new HashMap<Character,Integer>();
for(int i = 0;i < n;i++){
if(aa.containsKey(a[i])){
if(!bb.containsKey(b[i])){
return false;
}else{
int c = aa.get(a[i]);
int d = bb.get(b[i]);
if(c != d){
return false;
}else{
aa.remove(a[i]);
bb.remove(b[i]);
aa.put(a[i],i);
bb.put(b[i],i);
}
}
}else{
if(bb.containsKey(b[i])){
return false;
}else{
aa.put(a[i],i);
bb.put(b[i],i);
}
}
}
return true;
}
}
LeetCode(44)- Isomorphic Strings的更多相关文章
- leetcode:Isomorphic Strings
Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are isom ...
- [leetcode]205. Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- LeetCode 205 Isomorphic Strings
Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
- LeetCode 205. Isomorphic Strings (同构字符串)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 【leetcode】Isomorphic Strings
题目简述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...
- Java for LeetCode 205 Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 【leetcode】Isomorphic Strings(easy)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- (easy)LeetCode 205.Isomorphic Strings (*)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- Java [Leetcode 205]Isomorphic Strings
题目描述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...
随机推荐
- 关于在eclipse开发环境上打开手机data文件
使用Eclipse开发Android上的数据库应用,需要把数据库文件放到/data/data/mynamespace/database文件夹下,普通手机通过ROOT后经常还是看不到这个文件夹,这时需要 ...
- 6.5、Android Studio的Android Device Monitor
Android Device Monitor是一个独立的工具,可以对Android应用进行调试和分析.Android Device Monitor无需安装整合在一个IDE中,比如像Android St ...
- ExtJS学习(一)Ext自定义类实现
工作中项目需要extjs,所以学习一下,做个笔记防止遗忘了.以后回忆起来也方便. 首先下载extjs官网地址:http://extjs.org.cn/ 下载以后的目录结构: 先写一个入门的程序吧自定义 ...
- 聊聊Condition
本文可作为传智播客<张孝祥-Java多线程与并发库高级应用>的学习笔记. 上面我们说了Lock,那是对synchronized的一种更为面向对象的替代,在原来的synchronized内部 ...
- 1. React介绍 React开发环境搭建 React第一个程序
什么是 React React 是 Facebook 发布的 JavaScript 库,以其高性能和独特的设计理念受到了广泛关注. React的开发背景 Faceboo ...
- C++三目运算符的增强
<p>// 在C语言中表达式的结果放在寄存器中 // 在C语言中,表达式的返回值是变量的值 // 在C++中,表达式返回的是变量的本身</p><pre name=&quo ...
- 【Unity Shaders】Vertex & Fragment Shader入门
写在前面 三个月以前,在一篇讲卡通风格的Shader的最后,我们说到在Surface Shader中实现描边效果的弊端,也就是只对表面平缓的模型有效.这是因为我们是依赖法线和视角的点乘结果来进行描边判 ...
- 【OpenGL】理解一些基本问题
写在前面 啦啦啦,搞了很久的Unity Shaders,越学越觉得基础知识很重要.学Unity Shader的时候,总会想,shader到底是什么呢?shader的pipeline是什么呢?它们是怎么 ...
- Swift中实现Observable机制
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51917539 ...
- UE4帧动画Matineed
发一句牢骚,ue4除了渲染好一点,其他操作都没有unity便利,最近需要在项目中,调几个简单的动画使用到了Matineed,相当不好用.也可能是unity转ue4,有先入为主的观念,哈哈,never ...