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.

	 public static boolean isIsomorphic(String s, String t) {
if(s==null && t==null) return true;
else if(s.length()!=t.length()) return false;
Map<Character,Character> hm=new HashMap<Character,Character>();
for(int i=0;i<s.length();i++) {
if(hm.containsKey(s.charAt(i))) {
if(hm.get(s.charAt(i))!=t.charAt(i))
return false;
}
else if(hm.containsValue(t.charAt(i)))
return false; //开始的时候忘了这个条件,要注意一下
else
hm.put(s.charAt(i), t.charAt(i));
}
return true;
}

  

(String) 205.Isomorphic Strings的更多相关文章

  1. 205. Isomorphic Strings - LeetCode

    Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中 ...

  2. [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

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

  4. *205. Isomorphic Strings (string & map idea)

    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

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

  7. Java for LeetCode 205 Isomorphic Strings

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

  8. 205 Isomorphic Strings

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

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

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

随机推荐

  1. PHP 数组

    // 这里用数字来作为索引 $myArray = array(2012, 'blue', 5, 'BMW'); // 这个用关键字作为索引 $myAssocArray = array('year' = ...

  2. WP8.1 Study18:动态磁贴

    一.前言 动态磁贴在WindowsPhone8.1和Windows8.1都是其特色,有人喜欢有人讨厌,不过我觉得还是挺好的,可以让使用者很快知道App内的内容和吸引使用者打开App.下面来学习下怎样添 ...

  3. 记录一些容易忘记的属性 -- UIScrollView

    UIScrollView * sv = [[UIScrollView alloc] init]; //设置是否显示水平滚动条    sv.showsHorizontalScrollIndicator ...

  4. RSA IOS和Java

    整了三天 终于可以相互加密解密了,今天我给大家讲讲我遇到的大坑. 这篇文章只是做一个整理,帮大家理清一下步骤的而已 在ios端做证书 来实现我们和java的交流 需要4个文件. 一.首先,打开Term ...

  5. hasLayout与BFC的触发条件

    hasLayout与BFC是分别在IE和其他浏览器上的两个作用很相近的概念,在很多时候,我们需要触发它们去实现有些效果.例如清除浮动时需要触发hasLayout与BFC:很多自适应的两栏和三栏布局(两 ...

  6. HTML的总结

    一.Html简介    HTML 是一种标记语言       忽略大小写,语法宽松    使用 HTML 标记和元素,可以:       控制页面和内容的外观       发布联机文档       使 ...

  7. html元素中id和name的区别

    可以说几乎每个做过Web开发的人都问过,到底元素的ID和Name有什么区别阿?为什么有了ID还要有Name呢?! 而同样我们也可以得到最classical的答案:ID就像是一个人的身份证号码,而Nam ...

  8. C# 获取MAC地址

    /********************************************************************** * C# 获取MAC地址 * 说明: * 在C#中获取本 ...

  9. B-Tree算法分析与实现

    在数据库系统中,或者说在文件系统中,针对存储在磁盘上的数据读取和在内存中是有非常大的区别的,因为内存针对任意在其中的数据是随机访问的,然而从磁盘中读取数据是需要通过机械的方式来读取一个block,不能 ...

  10. 修改mysql用户名密码 和 PHPmysqlAdmin对应密码修改

    本地的mysql运行时,可能会用到修改用户名密码: mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('输入新密码');不存在修改用户啊 ...