[leetcode]205. Isomorphic Strings
2016-11-08 10:48
417 查看
题目链接:https://leetcode.com/problems/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
return true.
Given
return false.
Given
return true.
class Solution {
public:
string transferStr(string s){
char table[128] = {0};
char tmp = '0';
for (int i=0; i<s.length(); i++) {
char c = s.at(i);
if (table[c] == 0) {
table[c] = tmp++;
}
s[i] = table[c];
}
return s;
}
bool isIsomorphic(string s, string t) {
if (s.length() != t.length()) {
return false;
}
if (transferStr(s) == transferStr(t)) {
return true;
}
return false;
}
};
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.
class Solution {
public:
string transferStr(string s){
char table[128] = {0};
char tmp = '0';
for (int i=0; i<s.length(); i++) {
char c = s.at(i);
if (table[c] == 0) {
table[c] = tmp++;
}
s[i] = table[c];
}
return s;
}
bool isIsomorphic(string s, string t) {
if (s.length() != t.length()) {
return false;
}
if (transferStr(s) == transferStr(t)) {
return true;
}
return false;
}
};
相关文章推荐
- 【LeetCode】205. Isomorphic Strings
- LeetCode 205. Isomorphic Strings
- [leetcode] 205. Isomorphic Strings
- leetcode 205. Isomorphic Strings
- leetcode 205. Isomorphic Strings
- [LeetCode] 205. Isomorphic Strings 同构字符串
- LeetCode 205. Isomorphic Strings
- LeetCode笔记:205. Isomorphic Strings
- leetCode 205. Isomorphic Strings 哈希 字符串相似
- leetcode 205. Isomorphic Strings 巧妙使用hashmap.put返回值求解
- leetcode 205. Isomorphic Strings
- LeetCode 205. Isomorphic Strings
- Leetcode 205. Isomorphic Strings
- 【LeetCode】205. Isomorphic Strings
- [LeetCode] 205. Isomorphic Strings 解题思路 - Java
- [LeetCode]205. Isomorphic Strings
- Leetcode-205. Isomorphic Strings
- leetcode 205. Isomorphic Strings 同构字符串判断 + HashMap
- LeetCode 205. Isomorphic Strings
- leetcode-205. Isomorphic Strings