本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章

  1. Hacker Rank: Two Strings - thinking in C# 15+ ways

    March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...

  2. 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 ...

  3. Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  4. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  5. [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 ...

  6. [LeetCode] Group Shifted Strings 群组偏移字符串

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  7. [LeetCode] Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  8. [LeetCode] Multiply Strings 字符串相乘

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  9. 使用strings查看二进制文件中的字符串

    使用strings查看二进制文件中的字符串 今天介绍的这个小工具叫做strings,它实现功能很简单,就是找出文件内容中的可打印字符串.所谓可打印字符串的涵义是,它的组成部分都是可打印字符,并且以nu ...

随机推荐

  1. Android必知必会-长按返回健退出

    背景 平常比较常见的都是一定时间间隔内按两次返回键来退出应用,并且第一次点击会有相应的提示,网上资料比较多,这里写一下,长按返回键退出. 实现 实现的方案常用的有两个: 重写dispatchKeyEv ...

  2. 自制Linux重命名命令

    相比于Windows上的ren命名,Linux还真的是没有一个特定的重命名的命令.(虽然可以间接的使用mv来实现).下面我就来自己写一个简单的重命名命令. 准备工作 操作系统: Linux内核的系统都 ...

  3. JAVA面向对象-----接口与类、接口之间的关系

    接口与类.接口之间的关系 大家之前都知道类与类之间的关系继承,那么接口与类之间又是怎样子的关系呢? 接口与类之间是实现关系.非抽象类实现接口时,必须把接口里面的所有方法实现.类实现接口用关键字impl ...

  4. Hibernate系列学习之(二) 多对一、一对一、一对多、多对多的配置方法

    这是近期做案件录入.java项目中体会的几点:项目理解很是深刻,和大家共勉! hihernate一对多关联映射(单向Classes----->Student) 一对多关联映射利用了多对一关联映射 ...

  5. 最简单的基于librtmp的示例:发布H.264(H.264通过RTMP发布)

    ===================================================== 最简单的基于libRTMP的示例系列文章列表: 最简单的基于librtmp的示例:接收(RT ...

  6. python 访问 zookeeper

    python 访问 zookeeper zookeeper 分布式服务框架是 Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务.状态同 ...

  7. XML文档操作之JAXP下实现

    JAXP是java API for xml PRocessing的缩写. 其API可以在javax.xml.parsers 这个包中找到.这个包向用户提供了两个最重要的工厂类,SAXParserFac ...

  8. Cocos2D:塔防游戏制作之旅(七)

    用这3个变量,你可以创建多种不同类型的炮塔,它们可以有着不同的攻击属性,比如长距离重型攻击力,但是慢速攻击的炮塔,或者是渴望快速攻击但是攻击范围近的炮塔. 最后,代码包括了一个draw方法,它在炮塔周 ...

  9. ROS_Kinetic_12 ROS程序基础Eclipse_C++(三)usb camera

    ROS_Kinetic_12 ROS程序基础Eclipse_C++(三)usb camera 软件包下载地址:https://github.com/bosch-ros-pkg/usb_cam 下载后, ...

  10. Linux多线程实践(3) --线程属性

    初始化/销毁线程属性 int pthread_attr_init(pthread_attr_t *attr); int pthread_attr_destroy(pthread_attr_t *att ...