Lettcode_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());
	}
}

时间: 2025-01-17 19:46:00

Lettcode_205_Isomorphic Strings的相关文章

43. Multiply Strings

1. 问题描述 Given two numbers represented as strings, return multiplication of the numbers as a string.Note:The numbers can be arbitrarily large and are non-negative.Converting the input string to integer is NOT allowed.You should NOT use internal librar

Multiply Strings

package cn.edu.xidian.sselab;/** * title:Multiply Strings * content: * Given two numbers represented as strings, return multiplication of the numbers as a string. * Note: The numbers can be arbitrarily large and are non-negative. * 读已知条件可以,两个String转换

leetcode笔记: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 chara

LeetCode:Isomorphic Strings

1.题目名称 Isomorphic Strings(同构的字符串) 2.题目地址 https://leetcode.com/problems/isomorphic-strings/ 3.题目内容 英文: 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 occurre

poj 2406 Power Strings KMP

Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative inte

go语言学习 strings常用函数

strings包中的函数用法 参考链接http://studygolang.com/articles/88 1.strings.replace() 函数原型 func Replace(str1, old, str2, n int) string //old是str1中的字符串,用str2替换str1中的old,一共替换n个.如果n<0,则全部替换 fmt.Println(strings.Replace("tet tet tet", "e", "es&

strings用法小记

By francis_hao    Feb 14,2017 打印文件中可打印字符,每个序列至少四(可配置)个字符长.主要用于显示非文本文件 概述 选项解释 -a --all - 扫描整个文件,不管那些段是否被加载或初始化,一般此项是默认的,除非程序被配置成-d的模式 -d --data 只打印初始化过或加载过的部分,此项可以减少一些垃圾数据. -f --print-file-name 在打印字符之前打印文件名 -min-len -n min-len --bytes=min-len 指定打印的最短

Power Strings(KMP)

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 45008   Accepted: 18794 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "

[shiro] Wildcard string cannot be null or empty. Make sure permission strings are properly formatted.

访问某页面时,出现了这个异常: java.lang.IllegalArgumentException: Wildcard string cannot be null or empty. Make sure permission strings are properly formatted. at org.apache.shiro.authz.permission.WildcardPermission.setParts(WildcardPermission.java:154) at org.apa