LeetCode Isomorphic Strings

原创 2015年07月09日 00:59:35

Description:

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.

Solution:

分别用两个数组记录,两个字符串的字符,在前一次遇到这个字符的下标。如果所有的字符的下标相同,则返回true,否则false。

import java.util.*;

public class Solution {
	public boolean isIsomorphic(String s, String t) {

		int prePosition1[] = new int[1000];
		int prePosition2[] = new int[1000];

		Arrays.fill(prePosition1, -1);
		Arrays.fill(prePosition2, -1);

		int ch1, ch2;

		for (int i = 0; i < s.length(); i++) {
			ch1 = s.charAt(i);
			ch2 = t.charAt(i);
			if (prePosition1[ch1] == prePosition2[ch2]) {
				prePosition1[ch1] = i;
				prePosition2[ch2] = i;
			} else {
				return false;
			}

		}

		return true;
	}
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Leetcode28: Isomorphic Strings

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

[leetcode]Isomorphic Strings

Isomorphic Strings   Given two strings s and t, determine if they are isomorphic. Two strin...

LeetCode | Isomorphic Strings

题目 Given two strings s and t, determine if they are isomorphic. Two strings are isomorph...

LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)

翻译给定两个字符串s和t,决定它们是否是同构的。如果s中的元素被替换可以得到t,那么称这两个字符串是同构的。在用一个字符串的元素替换另一个字符串的元素的过程中,所有字符的顺序必须保留。 没有两个字符可...
  • NoMasp
  • NoMasp
  • 2016-01-30 14:16
  • 2649

<LeetCode><Easy> 205 Isomorphic Strings --HashTable

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

leetcode 205. Isomorphic Strings-字符匹配|哈希表

原题链接:205. Isomorphic Strings 【思路】 需要用到一个 HashMap数据结构, map 中存储这样一对映射:用 s 中的每个字符作为 map 的 key, t 中的每个字符...

【leetcode】Isomorphic Strings

Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings ...

Leetcode 205. Isomorphic Strings

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

LeetCode_Isomorphic Strings

Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are ...

[LeetCode]Isomorphic Strings

QuestionGiven two strings s and t, determine if they are isomorphic.Two strings are isomorphic if th...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)