leetcode:Isomorphic Strings
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的更多相关文章
- [leetcode]205. Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- LeetCode之“散列表”:Isomorphic Strings
题目链接 题目要求: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic i ...
- LeetCode OJ:Isomorphic Strings(同构字符串)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- LeetCode 205. Isomorphic Strings (同构字符串)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- LeetCode 205 Isomorphic Strings
Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
- 【leetcode】Isomorphic Strings
题目简述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...
- Java for LeetCode 205 Isomorphic Strings
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 【leetcode】Isomorphic Strings(easy)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- (easy)LeetCode 205.Isomorphic Strings (*)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
随机推荐
- git学习——<一>git安装
一.windows.linux平台安装 windows平台安装简单方便,到git官网上下载exe安装包即可,会把git bash shell给你安装好,你到命令窗口便可直接使用. linux平台安装, ...
- Task相关
1.Task的优势: 1)把任务当成变量来用,可以作为参数而传递: 2)可以捕获到异步操作中发生的异常. 2.开始异步 Task.Factory.StartNew(() => Thread.Sl ...
- sublime text3 插件安装
安装Package control 先打开安装代码的命令行 按 ctrl+~或者 view -> show console 将下面的代码粘贴到输入框里 按回车 import urllib.re ...
- 关于面向对象--oop
这两天在做大数据方面的项目看到关于job作业调度的设计,扣了两天了,感触良多,记下来做个反省. 这是一个精简版的图,其中还有一些没有划到,其实到这里目前对我来说已经足够了. 看完图之后进行分析,我只抛 ...
- Mysql 操作
MySQL命令行导出数据库:1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录如我输入的命令行:cd C:\Program Files\MySQL\MySQL Server ...
- javascript实现数据结构:线性表--简单示例及线性表的顺序表示和实现
线性表(linear list)是最常用且最简单的一种数据结构.一个线性表是n个数据元素的有限序列.在稍复杂的线性表中,一个数据元素可以由若干个数据项(item)组成. 其中: 数据元素的个数n定义为 ...
- POJ 1961 2406 (KMP,最小循环节,循环周期)
关于KMP的最短循环节.循环周期,请戳: http://www.cnblogs.com/chenxiwenruo/p/3546457.html (KMP模板,最小循环节) POJ 2406 Powe ...
- cojs QAQ的图论题 题解报告
话说这个题目应该叫做 斯特林数的逆袭 QAQ 先说一说部分分的算法 1.n<=5 直接暴力搜索就可以了 2.k=0的时候不难发现任意一张图的价值都是n,问题转化为计算有多少种图,显然是2^C(n ...
- 李洪强iOS开发之计算数组的最大最小值
// // ViewController.m // A21 - 李洪强 - 输出参数 // // Created by vic fan on 16/7/3. // Copyright © 20 ...
- 本地替换文件读取MYSQL密码
Mysql 的密码默认是存储在/data/mysql/下面的三个文件中:user.MYD,user.frm,user.MYI 先把这三个文件下载到本地,然后替换本地的这三个文件 使用net stop ...