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.

Note:
You may assume both s and t have the same length.


题目标签:Hash Table

  题目给了我们两个string, 让我们判断它们是不是isomorphic。

  只有当两个string的 char 是 1对1 map的时候,才是isomorphic,那么来看一下两个情况,它们不是 1对1 map:

    1. bar  foo

     map:

      b -> f

      a -> o

      r  -> o

      这种情况就是 左边两个chars  map 到了 同一个 右边的 char;

    2. bb  fa

     map:

      b -> f

      b -> a

      这种情况就是 右边两个chars map 到了 同一个 左边的char;

  所以只要遇到这两种情况,返回false,剩下的就是true。

Java Solution:

Runtime beats 57.23%

完成日期:05/26/2017

关键词:HashMap

关键点:利用HashMap来记录1对1map

 class Solution
{
public boolean isIsomorphic(String s, String t)
{
HashMap<Character, Character> map = new HashMap<>(); for(int i=0; i<s.length(); i++)
{
char sChar = s.charAt(i);
char tChar = t.charAt(i); if(map.containsKey(sChar))
{
if(!map.get(sChar).equals(tChar)) // case 1: two different right chars map to one left char
return false;
}
else
{
if(map.containsValue(tChar)) // case 2: two different left chars map to one right char
return false; map.put(sChar, tChar);
} } return true;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

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. [leetcode]205. Isomorphic Strings同构字符串

    哈希表可以用ASCII码数组来实现,可以更快 public boolean isIsomorphic(String s, String t) { /* 思路是记录下每个字符出现的位置,当有重复时,检查 ...

  3. 205 Isomorphic Strings 同构字符串

    给定两个字符串 s 和 t,判断它们是否是同构的.如果 s 中的字符可以被替换最终变成 t ,则两个字符串是同构的.所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一个字 ...

  4. [LeetCode] 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(同构的字符串)(string、vector、map)(*)

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

  6. Leetcode 205 Isomorphic Strings 字符串处理

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

  7. LeetCode 205 Isomorphic Strings

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

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

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

  9. Java for LeetCode 205 Isomorphic Strings

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

随机推荐

  1. python面向对象之封装

    一.封装 优点:(1)将变化隔离          (2)封装使用         (3)提高复用性        (4)提高安全性 封装原则:(1)将不需要对外提供的内容都隐藏起来         ...

  2. JDBC第三篇--【事务、元数据、改造JDBC工具类】

    这是我JDBC的第一篇 http://blog.csdn.net/hon_3y/article/details/53535798 这是我JDBC的第二篇 http://blog.csdn.net/ho ...

  3. 泛型在Web中的作用

    当我们写网页的时候,常常会有多个DAO,我们要写每次都要写好几个DAO,这样会有点麻烦. 那么我们想要的效果是什么呢??只写一个抽象DAO,别的DAO只要继承该抽象DAO,就有对应的方法了. 要实现这 ...

  4. phoenix

    phoenix(直译做凤凰)的操作sql是通过jdbc发送到HBase的.phoenix的查询语句会转化为hbase的scan操作和服务器端的过滤器.如果我们手工使用HBase的api去写这些代码,也 ...

  5. kettle的HTTPPOST控件发送WSDL的webservice请求配置

    1.webservice请求的URL:http://pubservice.rjhn.com.cn/AppserviceTest/JsonWcfService.svc?WSDL 2.使用SOAPUI测试 ...

  6. 西邮linux兴趣小组2014纳新免试题(四)

    [第四关] 题目 http://findakey.sinaapp.com/ Example: String1:FFFF8 5080D D0807 9CBFC E4A04 24BC6 6C840 49B ...

  7. DialogFragment的应用

    一.DialogFragment简单介绍: 1.基本概念 DialogFrament 指一个与fragment建立了关联的Dialog, 随fragment生, 随fragment死, 即Dialog ...

  8. Apache Spark 2.2.0 中文文档 - Submitting Applications | ApacheCN

    Submitting Applications 在 script in Spark的 bin 目录中的spark-submit 脚本用与在集群上启动应用程序.它可以通过一个统一的接口使用所有 Spar ...

  9. CenOs 部署记录

    1.安装APache.即 httpd 2.需要将80端口添加进iptable.外网才能访问.命令:iptables -I INPUT -p TCP --dport 80 -j ACCEPT

  10. spring web.xml配置

    <!--推荐使用此种方式-->  <listener> <listener-class> org.springframework.web.context.Conte ...