Leetcode_205_Isomorphic Strings
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46530865
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.
思路:
(1)题意为给定两个长度相同的字符串,判断这两个字符串中相同字符位置上是否相对应。
(2)要判断相同字符的位置是否相对应,需要记录所有字符出现的位置。首先,创建两个不同的Map分别来保存两个字符串相关信息,其中key为字符串中的字符,value为该字符在字符串中的下标(下标以字符串形式保存),例如:egg和add的保存形式分别为{e={"1"},g={"23"}}和{a={"1"},d={"23"}}。其次,只需要遍历字符数组中的每一个字符,如果Map对应的key中不包含当前遍历的字符,则将该字符及其位置存入Map中,否则,则从Map中取出当前字符对应的value,将当前字符位置追加到value上。最后,遍历完字符数组后,需要判断两个Map所对应value大小是否相同,不相同则返回false,否则,分别将两个Map中value值依次放入两个新的StringBuffer中,如果最后得到的字符串内容相同,则返回true,否则返回false。例如:egg和add最后得到的字符串都为“123”,而pick和good对应的Map为{p={"1"},i={"2"},c={"3"},k={"4"}}和{g={"1"},o={"23"},d={"4"}},显然两个Map对应value大小不同,所以返回false。
(3)详情将下方代码。希望本文对你有所帮助。
算法代码实现如下:
package leetcode;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
/**
*
* @author liqq
*
*/
public class Isomorphic_Strings {
public static boolean isIsomorphic(String s, String t) {
char[] ch1 = s.toCharArray();
char[] ch2 = t.toCharArray();
int len = ch1.length;
Map<Character, StringBuffer> _map1 = new LinkedHashMap<Character, StringBuffer>();
Map<Character, StringBuffer> _map2 = new LinkedHashMap<Character, StringBuffer>();
for (int i = 0; i < len; i++) {
if(_map1.get(ch1[i])==null){
_map1.put(ch1[i], new StringBuffer());
_map1.get(ch1[i]).append(i);
}else{
_map1.get(ch1[i]).append(i);
}
if(_map2.get(ch2[i])==null){
_map2.put(ch2[i], new StringBuffer());
_map2.get(ch2[i]).append(i);
}else{
_map2.get(ch2[i]).append(i);
}
if(_map1.values().size()!=_map2.values().size()){
return false;
}
}
StringBuffer b1 = new StringBuffer();
StringBuffer b2 = new StringBuffer();
for (Iterator<StringBuffer> iterator1 = _map1.values().iterator(),iterator2 = _map2.values().iterator();
iterator1.hasNext()&&iterator2.hasNext();) {
b1.append(iterator1.next());
b2.append(iterator2.next());
}
return b1.toString().equals(b2.toString());
}
}
Leetcode_205_Isomorphic Strings的更多相关文章
- 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 ...
- 使用strings查看二进制文件中的字符串
使用strings查看二进制文件中的字符串 今天介绍的这个小工具叫做strings,它实现功能很简单,就是找出文件内容中的可打印字符串.所谓可打印字符串的涵义是,它的组成部分都是可打印字符,并且以nu ...
随机推荐
- 剑指Offer——小米+小红书笔试题+知识点总结
剑指Offer--小米+小红书笔试题+知识点总结 情景回顾 时间:2016.9.23 19:00-21:00 2016.9.24 15:00-17:00 地点:山东省网络环境智能计算技术重点实验室 事 ...
- 通过一个例子了解MapReduce
写MapReduce程序的步骤: 把问题转化为MapReduce模型: 设置运行参数: 写map类: 写reduce类: 例子:统计单词个数 Map的任务是将内容用" "分开,然后 ...
- android listview 使用
今天在做项目的时候用了自定义listview以及自定义的item.adapter.现在把其中需要注意的地方记录下来: 1.item内如果有button等控件时,在监听listview的onitemcl ...
- J-Robot,能走、能跳舞的机器人
最近一个月基本上没有更新博客了,主要是和朋友一起在捣鼓J-Robot这个机器人,现在基本是可以控制它了,也算是一点小小的成就感吧. 先来几张图片吧. 再来一张: 是否觉得呆呆的?来,Jim ...
- 【Netty源码分析】Reactor线程模型
1. 背景 1.1. Java线程模型的演进 1.1.1. 单线程 时间回到十几年前,那时主流的CPU都还是单核(除了商用高性能的小机),CPU的核心频率是机器最重要的指标之一. 在Java领域当时比 ...
- Xcode7.2中如何添加一个Empty Application模板
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) Xcode 6.0正式版之后已经没有所谓的Empty Appl ...
- 学习笔记-JS公开课一
JS公开课笔记 没特别说明就是和Java语言一样. JS变量:弱类型语言 1.在JS中,true表示1,false表示0.和Java不一样. 2. var y: 提示undefined: 3.如果al ...
- Inventory Transactions Manager
Overview Inventory Transaction Manager用于处理库存接口表(MTL_TRANSACTION_INTERFACE或者MTL_MATERIAL_TRANSACTIONS ...
- 如何在Cocos2D游戏中实现A*寻路算法(一)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- 四大组件之Activity小结
一大波面试就要靠近,赶紧总结总结一些基础问题 1.Activity的概念 是Android应用层开发的四大组件之一,主要负责和用户交互部分,有自己的生命周期,在其上可以布置按钮,文本框等各种控件,简单 ...