LeetCode OJ: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.
看两个字符串是否同构,用map就可以解决,不过要注意双向都要检查,代码如下:
class Solution {
public:
bool isIsomorphic(string s, string t) {
map<char, char> m1;
map<char, char> m2;
if(s.size() != t.size())
return false;
for(int i = ; i < s.size(); ++i){
if(m1.find(s[i]) == m1.end() && m2.find(t[i]) == m2.end()){
m1[s[i]] = t[i];
m2[t[i]] = s[i];
}
else if(m1.find(s[i]) != m1.end() && m2.find(t[i]) != m2.end()){
if(m1[s[i]] != t[i] || m2[t[i]] != s[i])
return false;
}else{
return false;
}
}
return true;
}
};
LeetCode OJ: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]205. Isomorphic Strings同构字符串
哈希表可以用ASCII码数组来实现,可以更快 public boolean isIsomorphic(String s, String t) { /* 思路是记录下每个字符出现的位置,当有重复时,检查 ...
- [LeetCode] Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 205 Isomorphic Strings 同构字符串
给定两个字符串 s 和 t,判断它们是否是同构的.如果 s 中的字符可以被替换最终变成 t ,则两个字符串是同构的.所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不能映射到同一个字 ...
- leetcode:Isomorphic Strings
Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are isom ...
- LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)
翻译 给定两个字符串s和t,决定它们是否是同构的. 假设s中的元素被替换能够得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换还有一个字符串的元素的过程中.所有字符的顺序必须保留. 没有两 ...
- 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 字符串处理
判断两个字符串是否同构 hs,ht就是每个字符出现的顺序 "egg" 与"add"的数字都是122 "foo"是122, 而"ba ...
- LeetCode Isomorphic Strings 对称字符串
题意:如果两个字符串是对称的,就返回true.对称就是将串1中的同一字符都一起换掉,可以换成同串2一样的. 思路:ASCII码表哈希就行了.需要扫3次字符串,共3*n的计算量.复杂度O(n).从串左开 ...
- LeetCode 205 Isomorphic Strings
Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
随机推荐
- 【转】js获取当前日期时间“yyyy-MM-dd HH:MM:SS”
获取当前的日期时间 格式“yyyy-MM-dd HH:MM:SS” 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 function getNowFormatD ...
- Scala List 用法
1.++[B] 在A元素后面追加B元素 scala> val a = List(1) a: List[Int] = List(1) scala> val b = List(2) b: ...
- JS正则表达式从入门到入土(4)—— 预定义类与边界
预定义类 正则表达式提供预定义类来匹配常见的字符类 字符 等价类 含义 . [^\r\n] 除了回车符和换行符以外的所有字符 \d [0-9] 数字字符 \D [^0-9] 非数字字符 \s [\t\ ...
- C++ 顺序表实现
线性表就是字面上的意思, 顺序表是线性表基于数组的一种实现, “顺序”这个名字怎么来的并不清楚,可以勉强解释为“存储地址是连续.顺序的”. 另外两种线性表实现分别是“基于链表”和“散列存储”. 顺序表 ...
- CSS Text(文本)
CSS Text(文本) 一.文本颜色 color 颜色属性被用来设置文字的颜色. 颜色是通过CSS最经常的指定: 十六进制值 - 如: #FF0000 一个RGB值 - 如: RGB(255,0,0 ...
- nginx结合fastcgi
1.首先安装nginx,这里采用编译安装 useradd -M -s /sbin/nologin nginx 安装一些依赖包: yum -y install pcre-devel libxslt-de ...
- 尝试编辑java程序
尝试编译java程序 之前在用软件eclipse编译过简单的hello java程序,因为软件操作简单,后来学会了用命令符来编译程序.代码如下 public class abc { ...
- AD9361
AD9361框图 1. Fir滤波器的阶数为64或128 而内插或抽取因子为:1.2或4. HB1和HB2的内插或抽取因子为1或2而HB3的因子为1.2或3 BB_LPF为:三阶巴特沃斯低通滤波器 ...
- bzoj 3657 斐波那契数列(fib.cpp/pas/c/in/out)
空间 512M 时限2s [题目描述] 有n个大于1的正整数a1,a2,…,an,我们知道斐波那契数列的递推式是f(i)=f(i-1)+f(i-2),现在我们修改这个递推式变为f(i)=f(i-1) ...
- bootstrap && bagging && 决策树 && 随机森林
看了一篇介绍这几个概念的文章,整理一点点笔记在这里,原文链接: https://machinelearningmastery.com/bagging-and-random-forest-ensembl ...