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.

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

分析:同构字符串必须满足,s字符串中相同的字符对应t字符串中相同的字符,s字符串中不同的字符对应t字符串中不同的字符。

代码如下:

class Solution {
public:
bool isIsomorphic(string s, string t) {
char map_s[128] = { 0 };
char map_t[128] = { 0 };
int len = s.size();
for (int i = 0; i < len; ++i)
{
if (map_s[s[i]]!=map_t[t[i]]) return false;
map_s[s[i]] = i+1;
map_t[t[i]] = i+1;
}
return true;
}
};

可参考解法:

记录遍历s的每一个字母,并且记录s[i]到t[i]的映射,当发现与已有的映射不同时,说明无法同构,直接return false。然后交换s和t的位置再重来一遍,这样保证s和t之间的双向映射。

 class Solution {
public:
bool isIsomorphic(string s, string t) {
if (s.length() != t.length()) return false;
map<char, char> mp;
for (int i = 0; i < s.length(); ++i) {
if (mp.find(s[i]) == mp.end()) mp[s[i]] = t[i];
else if (mp[s[i]] != t[i]) return false;
}
mp.clear();
for (int i = 0; i < s.length(); ++i) {
if (mp.find(t[i]) == mp.end()) mp[t[i]] = s[i];
else if (mp[t[i]] != s[i]) return false;
}
return true;
}
};

题意为 判断一个字符串中是否可以由另一个字符串中的字符替换而来。

如果直接尝试替换的话实现比较麻烦,可以对两个字符串分解进行转换,看转换后的结果是否一致。 

依次用‘0’, ‘1‘...替换字符串出现的字符,如 ’abbc‘可以替换为’0112‘。需要设置一张转换表,记录转换后每个字符对应的替代字符:

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;
}
};

其他:

class Solution {
public:
bool isIsomorphic(string s, string t)
{
const size_t n = s.size();
if ( n != t.size())
return false; unsigned char forward_map[256] = {}, reverse_map[256] = {}; for ( int i=0; i < n; ++i)
{
unsigned char c1 = s[i];
unsigned char c2 = t[i]; if ( forward_map[c1] && forward_map[c1] != c2)
return false; if ( reverse_map[c2] && reverse_map[c2] != c1)
return false; forward_map[c1] = c2;
reverse_map[c2] = c1;
} return true;
}
};

3 lines 3ms C solution

bool isIsomorphic(char* s, char* t) {
static char n[512],*m = n + 256;
return (!*s && !*t && memset(n,0,512)) || (((!(m[*s] || n[*t] || !(m[*s] = *t, n[*t] = *s)) ||
(m[*s] == *t && n[*t] == *s)) || !memset(n,0,512)) && isIsomorphic(s+1,t+1)); }

3ms O(n) C solution:The idea is to use two arrays to store the mappings between corresponding characters of s and t.

bool isIsomorphic(char* s, char* t) {
char map[256], rmap[256]; memset(map, 0, sizeof map);
memset(rmap, 0, sizeof rmap);
for ( ; *s; ++s, ++t)
if (!map[*s]) {
if (rmap[*t]) // another character already maps to *t
return false; map[*s] = *t;
rmap[*t] = *s;
} else if (map[*s] != *t)
return false; return true;
}

  

  

  

leetcode: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之“散列表”:Isomorphic Strings

    题目链接 题目要求: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic i ...

  3. LeetCode OJ:Isomorphic Strings(同构字符串)

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

  4. LeetCode 205. 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

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

  6. 【leetcode】Isomorphic Strings

    题目简述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...

  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. 【leetcode】Isomorphic Strings(easy)

    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. sampleGradient(sampler,uv,dds,ddy)

    vsm里面用这个梯度采样 采放了z,z*z的shadowmap 这种采样方式和普通sample有什么区别

  2. ajax跨域请求,页面和java服务端的写法

    方法一(jsonp): 页面ajax请求的写法: $.ajax({ type : "get", async : false, cache : false, url : " ...

  3. How Big Is A Petabyte, Exabyte, Zettabyte, Or A Yottabyte?

    一个关于Byte递增的描述,蛮有趣的! Bytes -> Kilobyte -> Megabyte -> Gigabyte -> Terabyte -> Petabyte ...

  4. vuforia 结合 unity3d 开发 AR 的 androidAPP 总结

    原地址:https://software.intel.com/zh-cn/blogs/2014/07/09/vuforia-unity3d-ar-androidapp/?utm_campaign=CS ...

  5. Long和Date数据类型之间相互转换代码 - 调整时间推前往后,截取long型日期方法。

    SimpleDateFormat DATETIME_SEC_STR = new SimpleDateFormat("yyyyMMddHHmmss"); SimpleDateForm ...

  6. *args和**kw魔法参数

    学Python挺久了,现在才搞懂这个还是有点惭愧 *args:传入元组,无关键字 **kw:传入字典,有关键字 示例: *args **kw 一起使用时args的参数需在前:

  7. vi/vim使用指北 ---- Moving Around in a Hurry

    上一篇文章中,简单列出了一些基本的Vim操作,也列出了很多的光标移动命令,本章主要是有哪些命令可以更快的移动光标. vim的编辑操作,用得最多就是移动光标,对于很少行的文件来说,基本的命令就够用了,但 ...

  8. 【中国互联网不眠夜】Struts2漏洞百出,OneRASP鼎力相助

    Struts2是一款优秀的网站框架,在互联网上有十分广泛的应用,近期apache官方发布了高危漏洞通告Apache Struts 任意代码执行漏洞(CVE-2016-3081,S2-032),该漏洞风 ...

  9. jdom处理的XML Document 和String 之间的相互转化

    package util; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileWriter; ...

  10. Android中两种设置全屏的方法

    设置全屏的两种方法: 第一种:在配置文件里面配置: <?xml version="1.0" encoding="utf-8"?><manife ...