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.

思路: 注意本题是一一对应的关系,而hashmap只能保证索引值唯一,即s到t是唯一的,但是可能存在2个s对应同一个t,所以增加了set加以判断

class Solution {
public:
bool isIsomorphic(string s, string t) {
int len = s.length();
if(len == ) return true; map<char,char> c_map;
set<char> c_set; //to avoid two character in s maps to the same character in t
for(int i = ; i < len; i++){
if(c_map.find(s[i]) == c_map.end()){
if(c_set.find(t[i]) == c_set.end()){
c_map[s[i]] = t[i];
c_set.insert(t[i]);
}
else return false;
}
else if(c_map[s[i]]!=t[i]) return false;
}
return true;
}
};

205. Isomorphic Strings (Map)的更多相关文章

  1. 205. Isomorphic Strings - LeetCode

    Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中 ...

  2. [leetcode]205. Isomorphic Strings 同构字符串

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

  3. 【刷题-LeetCode】205. Isomorphic Strings

    Isomorphic Strings Given two strings *s* and *t*, determine if they are isomorphic. Two strings are ...

  4. *205. Isomorphic Strings (string & map idea)

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

  5. LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)

    翻译 给定两个字符串s和t,决定它们是否是同构的. 假设s中的元素被替换能够得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换还有一个字符串的元素的过程中.所有字符的顺序必须保留. 没有两 ...

  6. LeetCode 205 Isomorphic Strings

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

  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. (String) 205.Isomorphic Strings

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

  9. 205 Isomorphic Strings

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

随机推荐

  1. VS2017创建一个 ASP.NET Core2.0 应用,并搭建 MVC 框架

    https://testerhome.com/topics/11747 1.使用最新版本的VS2017,并安装.NET Core2.0中相关开发工具   2.打开VS2017,点击文件-新建-项目,选 ...

  2. leetcode989

    class Solution: def addToArrayForm(self, A, K): i = len(A) - 1 while i >= 0 and K > 0: A[i] += ...

  3. MySQL 创建自定义函数

    语法:Create function function_name(参数列表)returns返回值类型 函数体 函数名,应合法的标识符,不应与系统关键字冲突. 一个函数应该属于某个数据库,可以使用db_ ...

  4. IIS快捷方式

    一般打开IIS管理器的方式 都是 计算机->管理->服务应用程序->Internet应用程序管理器 这样一步就可以 打开IIS了

  5. python 读取文件

    python 一次读取多行 with open(filename, "r") as f: lines = f.readlines(LINE_BATCH) while lines: ...

  6. 尚硅谷redis学习8-事务

    是什么? 能干嘛? 常用命令 案例说明 1.正常执行 2.放弃事务 3.全部放弃(全体连坐) 4.只抛弃错误(冤头债主) 5.watch监控 悲观锁 悲观锁(Pessimistic Lock), 顾名 ...

  7. JS 打印图片

    在使用window.print()进行打印时,打印的内容可能会包含图片内容,此时的图片内容不能设置为背景图片,否则将无法再打印页面显示. <!doctype html> <html& ...

  8. 拓展jquery js动态添加html代码 初始化数据

    1 /** * 新增数据筛选 */ (function () { $.filterEvent = function(options){ var _this = this; var defaults = ...

  9. MySQL中tinytext、text、mediumtext和longtext详解【转】

    一.数字类型 类型 范围 说明   Char(N) [binary] N=1~255 个字元binary :分辨大小写 固定长度 std_name cahr(32) not null VarChar( ...

  10. SQLServer 的数据分页:

    假设现在有这样的一张表:CREATE TABLE test( id int primary key not null identity, names varchar(20))然后向里面插入大约1000 ...