题目描述:

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.

解题思路:

用数组做映射。注意每次字典映射的时候赋值为i+1,这是为了防止跟第一次映射时候的赋值混淆。(因为数组的初始化赋值为0,若每次赋值为i,那么第一次赋值的时候也为0,这样就混淆了)。

代码如下:

public class Solution {
public boolean isIsomorphic(String s, String t) {
int[] mapS = new int[128];
int[] mapT = new int[128];
for(int i = 0; i < s.length(); i++){
if(mapS[s.charAt(i)] != mapT[t.charAt(i)])
return false;
mapS[s.charAt(i)] = mapT[t.charAt(i)] = i + 1;
}
return true;
}
}

  

Java [Leetcode 205]Isomorphic Strings的更多相关文章

  1. [leetcode]205. Isomorphic Strings 同构字符串

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

  2. Java for LeetCode 205 Isomorphic Strings

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

  3. [LeetCode] 205. Isomorphic Strings 解题思路 - Java

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

  4. LeetCode 205. Isomorphic Strings (同构字符串)

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

  5. LeetCode 205 Isomorphic Strings

    Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...

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

    翻译 给定两个字符串s和t,决定它们是否是同构的. 假设s中的元素被替换能够得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换还有一个字符串的元素的过程中.所有字符的顺序必须保留. 没有两 ...

  7. (easy)LeetCode 205.Isomorphic Strings (*)

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

  8. leetcode 205. Isomorphic Strings(哈希表)

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

  9. Leetcode 205 Isomorphic Strings 字符串处理

    判断两个字符串是否同构 hs,ht就是每个字符出现的顺序 "egg" 与"add"的数字都是122 "foo"是122, 而"ba ...

随机推荐

  1. text-align:-moz-center与text-align:-webkit-center区别与用法

    最近发现各浏览器的不兼容,关于text-align:center这个很多浏览器不兼容. 1.测试发现:text-align:center在IE下是管用的. 2.text-align:-moz-cent ...

  2. [设计模式] 3 创建者模式 builder

    转载http://blog.csdn.net/wuzhekai1985/article/details/6667467 建造者模式的定义将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不 ...

  3. PHP之mysql_real_escape_string()函数讲解

    定义和用法 mysql_real_escape_string() 函数转义 SQL 语句中使用的字符串中的特殊字符. 下列字符受影响: \x00 \n \r \ ' " \x1a 如果成功, ...

  4. MATERIALIZED VIEW

    Oracle的实体化视图提供了强大的功能,可以用在不同的环境中,实体化视图和表一样可以直接进行查询.实体化视图可以基于分区表,实体化视图本身也可以分区. 主要用于预先计算并保存表连接或聚集等耗时较多的 ...

  5. POJ3009Curling 2.0

    http://poj.org/problem?id=3009 题意 : 迷宫升级版,也是m*n的迷宫,0是可以走的,1是阻塞,2是初始点,3是目标位置,这个的阻塞是可以消除的,就是说只要石头撞到阻塞, ...

  6. oracle的全文索引

    1.查看oracle的字符集 SQL> select userenv('language') from dual; USERENV('LANGUAGE') ------------------- ...

  7. [主席树]HDOJ3874 Necklace

    题意:n个数 m个询问 询问的是[l, r]区间内不同的数的和 没有修改,静态的主席树即可 与 SPOJ QUERY 一样 将重复的元素建树即可 注意范围:$N \le  50000$ 每个值不超过1 ...

  8. hdu1017

    http://acm.hdu.edu.cn/showproblem.php?pid=1017 #include<iostream> #include<stdio.h> #inc ...

  9. 客户端一个http连接包含两个方向,一个是这个http连接的输入,另一个是这个http连接的输出。

    1.客户端一个http连接包含两个方向,一个是这个http连接的输入,另一个是这个http连接的输出. 利用httpclient进行ip地址和端口号连接后,http的输出端作为http请求参数设置.h ...

  10. mysql23个知识点

    1.它是一种解释语言:写一句执行一句,不需要整体编译执行. 2.1.没有“ ”,字符串使用‘ '包含 3.一个表只有一个主键,但是一个主键可以是由多个字段组成的 组合键 4.实体完整性:实体就是指一条 ...