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 ...
随机推荐
- c++之旅:类型的强制转换
类型强制转换 在编程的时候我们经常遇到类型的强制转换,C++为此提供了更安全的转换方式,在编程中我们更多的应该采用C++提供的类型转换方式 基本类型转换 基本类型转换用的最多,一般将高精度转换为低精度 ...
- myeclipse自动生成相应对象接收返回值的快捷键
在你要自动生成返回值对象的那一行的末尾(注意一定要将光标点到最后),按Alt+Shift+L:就可以了.
- Method Swizzling 剖析
一.背景介绍 关于Method Swizzling的文章一大堆,讲的非常好的也数不胜数.不过,很多人只是会用,知道一些注意点.深入一点问的话,估计就答得不好.归其原因就是对Method Swizzli ...
- JS的魅力
一.初探JavaScript魅力 基本知识: JavaScript是什么 网页特效原理 -JavaScript就是修改样式 编写JS流程 - 布局:HTML + CSS - 属性:确定修改哪些属性 - ...
- ss+proxifier灵活控制网络代理
SS相比大家都知道,不多说. proxifier可能知道的不是很多(至少在今天之前我是不知道的...可能我孤陋寡闻吧) 之前用ss基本上就是chrome SwitchyOmega+SS实现chrome ...
- 20145314郑凯杰 《Java程序设计》实验三 敏捷开发与XP实践实验报告
20145314郑凯杰 <Java程序设计>实验二 实验报告 实验要求 完成实验.撰写实验报告,实验报告以博客方式发表在博客园,注意实验报告重点是运行结果,遇到的问题(工具查找,安装,使用 ...
- 20145328 《Java程序设计》第10周学习总结
20145328 <Java程序设计>第10周学习总结 资料学习内容总结 网络编程 13.1 网络概述 网络编程技术是当前一种主流的编程技术,随着联网趋势的逐步增强以及网络应用程序的大量出 ...
- 20145331 《Java程序设计》第3周学习总结
20145331 <Java程序设计>第3周学习总结 教材学习内容总结 第四章 认识对象 •对象(Object):存在的具体实体,具有明确的状态和行为 •类(Class):具有相同属性和行 ...
- JavaScript高级程序设计-读书笔记(2)
第6章 面向对象的程序设计 创建对象 1.最简单方式创建Object的实例,如 var person = new Object(); person.name = “Greg”; person.age ...
- ubuntu如何U盘启 动制作
先到出 U盘的位置, 方法有很多,terminal 用root权限,采用fdisk -f 命令 unmount /dev/sdb -I //U盘拨出 sdb为U盘挂载的位置 mkfs.fat /dev ...