LeetCode 题解(197) : Isomorphic Strings
来源:互联网 发布:程序员岗位职责说明书 编辑:程序博客网 时间:2024/06/06 07:23
题目:
Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to gett.
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.
HashTable + used数组。
C++版:
class Solution {public: bool isIsomorphic(string s, string t) { unordered_map<char, char> n; vector<bool> used(95, false); for(int i = 0; i < s.length(); i++) { if(n.find(s[i]) != n.end()) { if(n[s[i]] != t[i]) return false; continue; } if(used[t[i] - ' ']) return false; n.insert(pair<char, char>(s[i], t[i])); used[t[i] - ' '] = true; } return true; }};
Java版:
import java.util.Hashtable;public class Solution { public boolean isIsomorphic(String s, String t) { Hashtable<Character, Character> m = new Hashtable<>(); boolean[] used = new boolean[95]; for(int i = 0; i < s.length(); i++) { if(m.containsKey(s.charAt(i))) { if(m.get(s.charAt(i)) != t.charAt(i)) return false; continue; } if(used[t.charAt(i) - ' ']) return false; m.put(s.charAt(i), t.charAt(i)); used[t.charAt(i) - ' '] = true; } return true; }}
Python版:
class Solution(object): def isIsomorphic(self, s, t): """ :type s: str :type t: str :rtype: bool """ d = {} used = [False] * 95 for i in range(len(s)): if s[i] in d: if d[s[i]] != t[i]: return False continue if used[ord(t[i]) - ord(" ")]: return False d[s[i]] = t[i] used[ord(t[i]) - ord(" ")] = True return True
0 0
- LeetCode 题解(197) : Isomorphic Strings
- LeetCode题解:Isomorphic Strings
- LeetCode题解:Isomorphic Strings
- leetcode题解之Isomorphic Strings
- leetcode题解-205.Isomorphic Strings && 290. Word Pattern
- 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
- Linux 中使用 clone 函数
- Android UI特效集锦
- awk用法
- 基于Ubuntu 14的hadoop全分布集群搭建
- Ajax提高篇(1)入门
- LeetCode 题解(197) : Isomorphic Strings
- 软工视频(九、十)
- java 资源大全
- 数据库事务 ios FMDB
- 开学季!MathType 6.9 序列号限时大促销进行中!
- oracle contains函数 全文检索
- 【笔试】28、二进制中1的个数
- 用消息队列模拟生产者消费者模型
- Android中的Intent(意图)的使用