205. 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 中的字符。不能一对多,也不能多对一。

利用两个map实现。

代码如下:

 class Solution {
public:
bool isIsomorphic(string s, string t) {
map<char, char> mapA;
map<char, char> mapB;
int n = s.length();
for(int i = ; i < n; i++)
{
char ss = s[i];
char tt = t[i];
map<char, char>::iterator it = mapA.find(ss);
if(it != mapA.end())
{
if(mapA[ss] != tt)
{
return false;
}
}
else
{
map<char, char>::iterator ii = mapB.find(tt);
if(ii != mapB.end() && mapB[tt] != ss)
{
return false;
}
else
{
mapA[ss] = tt;
mapB[tt] = ss;
}
}
}
return true;
}
};

leetcode 205的更多相关文章

  1. LeetCode 205 Isomorphic Strings

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

  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)

    205. 同构字符串 205. Isomorphic Strings

  4. Java实现 LeetCode 205 同构字符串

    205. 同构字符串 给定两个字符串 s 和 t,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序. ...

  5. Java for LeetCode 205 Isomorphic Strings

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

  6. Leetcode 205 Isomorphic Strings 字符串处理

    判断两个字符串是否同构 hs,ht就是每个字符出现的顺序 "egg" 与"add"的数字都是122 "foo"是122, 而"ba ...

  7. (easy)LeetCode 205.Reverse Linked List

    Reverse a singly linked list. 解法一:记录单链表每个节点的val,然后重新为单链表赋值.(取巧,仅仅是将val部分改变,原始node节点并没有改变) 代码如下: /** ...

  8. (easy)LeetCode 205.Isomorphic Strings (*)

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

  9. Java [Leetcode 205]Isomorphic Strings

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

随机推荐

  1. 手机设备连接eclipse的问题

    因为现在测试到的机型也没几台,很多都是直接能连接上eclipse,但是有些Android太“个性”所以遇到有一两台手机直接插上是不能连接到eclipse的:好了:解决问题把:首先我们需要知道连接的前提 ...

  2. 转载:bootstrap, boosting, bagging 几种方法的联系

    转:http://blog.csdn.net/jlei_apple/article/details/8168856 这两天在看关于boosting算法时,看到一篇不错的文章讲bootstrap, ja ...

  3. Ubuntu16.04 安装配置Caffe

    Caffe已经是第三次安装配置了,为什么是第三次呢?因为我实在是低估了深度学习对于硬件的要求.第一次我在自己笔记本上配置的单核,CPU only ...  结果是,样例数据跑了4小时,这还怎么玩?第二 ...

  4. .NET设计规范————命名规范

    NET设计规范:约定.惯用法与模式———命名规范 前言:          最近在看<.NET设计规范:约定.惯用法与模式>一书,主要还是讲.NET的设计规范,以前对这一块也不是特别在意, ...

  5. C#矩阵运算类库

    这个类库是本人参考许多相关资料之后做出的C#矩阵运算类库,因为C#的数值计算库相对比较少,所以希望这个类库能够给大家带来一些帮助. 源码github网址:https://github.com/Josh ...

  6. Index was outside the bounds of the array. (Microsoft.SqlServer.Smo)

    本地ssms是 安装Sqlserver 2008 r2 自带的 远端的server是sqlserver2014 可以连接,可以执行查询语句.但是,不能使用ssms生成对象的脚本.推测ssms 2008 ...

  7. onAttachedToWindow () 和 onDetachedFromWindow () (转)

    原文地址:http://blog.sina.com.cn/s/blog_74c22b2101012urv.html

  8. linux命令-cp/scp {拷贝}

    一 命令解释 名称:cp 使用权限:所有使用者 使用方式: cp [options] source dest cp [options] source... directory 命令参数: -a 尽可能 ...

  9. PHP常用库函数

    1.时间和日期 如何获取时间戳 time()--从1970年开始计算的毫秒数 echo time(); 日期 echo date('Y-m-d H:i:s'); 获取默认是时区 echo date_d ...

  10. 查看最点CPU的语句

    select c.last_execution_time,c.execution_count,c.total_logical_reads, c.total_logical_writes,c.total ...