leetcode--Isomorphic Strings
来源:互联网 发布:淘宝店铺层级划分 编辑:程序博客网 时间:2024/06/06 10:42
题目: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 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.
one:元素遍历,若s元素在Map里 s元素Map的键不是t元素false;若元素不在Map里在被映射的set里false,否则分别加入Map及set。
public class Solution { public boolean isIsomorphic(String s, String t) { if(s==null||t==null){return false;} if(s.length()!=t.length())return false; Map<Character,Character> map = new HashMap<Character,Character>(); Set<Character> set = new HashSet<Character>(); for(int i=0;i<s.length();i++){ char c1 = s.charAt(i); char c2 = t.charAt(i); if(map.containsKey(c1)){ if(map.get(c1)!=c2) return false; }else{ if(set.contains(c2))return false; else{ map.put(c1,c2); set.add(c2); } } } return true; }}
public class Solution { public boolean isIsomorphic(String s, String t) { Map m = new HashMap(); for (Integer i=0; i<s.length(); ++i) if (m.put(s.charAt(i), i) != m.put(t.charAt(i)+"", i)) return false; return true; }}
public class Solution { public boolean isIsomorphic(String sString, String tString) { char[] s = sString.toCharArray(); char[] t = tString.toCharArray(); int length = s.length; if(length != t.length) return false; char[] sm = new char[256]; char[] tm = new char[256]; for(int i=0; i<length; i++){ char sc = s[i]; char tc = t[i]; if(sm[sc] == 0 && tm[tc] == 0){ sm[sc] = tc; tm[tc] = sc; }else{ if(sm[sc] != tc || tm[tc] != sc){ return false; } } } return true; }}
class Solution {public: bool isIsomorphic(string s, string t) { int m1[256] = {0}, m2[256] = {0}, n = s.size(); for (int i = 0; i < n; ++i) { if (m1[s[i]] != m2[t[i]]) return false; m1[s[i]] = i + 1; m2[t[i]] = i + 1; } return true; }};public boolean isIsomorphic(String s1, String s2) { Map<Character, Integer> m1 = new HashMap<>(); Map<Character, Integer> m2 = new HashMap<>(); for(Integer i = 0; i < s1.length(); i++) { if(m1.put(s1.charAt(i), i) != m2.put(s2.charAt(i), i)) { return false; } } return true; }
0 0
- LeetCode 205 - Isomorphic Strings
- [LeetCode] Isomorphic Strings
- Isomorphic Strings Leetcode JAVA
- [LeetCode] Isomorphic Strings
- [LeetCode 205]Isomorphic Strings
- LeetCode 205-Isomorphic Strings
- leetcode Isomorphic Strings
- leetcode(c++) Isomorphic Strings
- leetcode--Isomorphic Strings
- [LeetCode]Isomorphic Strings
- LeetCode OJ Isomorphic Strings
- leetcode 205: Isomorphic Strings
- leetcode 205 Isomorphic Strings
- [leetcode] Isomorphic Strings
- 【leetcode】Isomorphic Strings
- [leetcode]Isomorphic Strings
- [LeetCode]Isomorphic Strings
- leetcode--Isomorphic Strings
- 我的第一篇博客
- python dict的一些简单用法
- 制作一个简单的照片查看器(支持缩放手势)
- Android studio约束布局 ConstraintLayout
- Spring 初探(一)(Spring起步实例)
- leetcode--Isomorphic Strings
- 微信公众平台开发--方倍工作室[申明:来源于网络]
- poj1236 Network of Schools (tarjan缩点+求入度为0的点和出度为0的点的个数)
- 使用POI读取和写出EXCEL文件(写出部分待完善)
- Redis Shell命令行操作五种value数据类型
- 让Xcode 8支持iOS 7设备
- Some Examples of PCIE Transactions
- C语言程序
- ajax 讲解(入门级)