本文是在学习中的总结,欢迎转载但请注明出处: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. Django extra 和 annotate

    >>> qs=Question.objects.extra(select={'anum': 'SELECT COUNT(*) FROM questions_answer WHERE ...

  2. Java编程思想阅读收获

    15年8月份买了一本Java编程思想第四版中文版.之所以买中文版是因为我试读了同事的英文版发现自己英语水平还是有限,单词虽然认识,但对很多句子把握不准,这样看书太慢了,要理解英文还要理解技术有些hol ...

  3. android SlidingmMenu的入门介绍

    最近了解了SlidingMenu控件的使用,之前手机qq等软件都采用了这种控件,所以还是很有必要学些这个控件的使用方法. 这个控件是基于github的一个开源项目. 地址是: https://gith ...

  4. Nginx的负载均衡 - 加权轮询 (Weighted Round Robin) 上篇

    Nginx版本:1.9.1 我的博客:http://blog.csdn.net/zhangskd 算法介绍 来看一个简单的Nginx负载均衡配置. http { upstream cluster { ...

  5. ORA-28001: the password has expired 详解 不用重启项目

    大早上正式库提示: Oracle提示错误消息ORA-28001: the password has expired 解决办法: 1.利用SYSDBA权限登陆: 2.查看账户信息:select user ...

  6. dbcp连接池不合理的锁导致连接耗尽

    应用报错,表象来看是连接池爆满了. org.springframework.transaction.CannotCreateTransactionException: Could not open J ...

  7. Android 5.1.1 源码目录结构

    点击打开链接 最近公司培训新同事,我负责整理一点关于android的基础知识,遥想当年,刚接触android,也是一头雾水, 啥都不懂,就是靠看文档和视频,对android有一个初步了解,然后就通过查 ...

  8. Android简易实战教程--第四话《最简单的短信发送器》

    首先配置一个布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmln ...

  9. Ubuntu 13.04设置root用户登录图形界面

    先切换到root用户, sudo su root 1.先设定一个root的密码, passwd root 2.备份一下lightgdm cp -p /etc/lightdm/lightdm.conf ...

  10. Java-IO之ByteArrayInputStream

    ByteArrayInputStream是字节数组输入流,继承于InputStream.它包含了一个内部缓冲区,该缓冲区包含从流中读取的字节,其实内部缓冲区就是一个字节数组,而ByteArrayInp ...