题目

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.

分析

判断给定的两个字符串是否同构,字符串字面上很容易就能看得出来;

但是怎么用算法来判断呢?

仔细分析:

e <——> a

g <——> d

g <——> d

感觉像离散数学中的双向映射。

在数据结构中可以选择map,选择unordered_map来存储字母映射关系,它的搜索性能为常量。

AC代码

class Solution {
public:
bool isIsomorphic(string s, string t) {
if (s.size() != t.size())
return false;
//求两个字符串的长度
int len = s.size(); //首先验证字符串s—>t的映射
unordered_map<char, char> um;
for (int i = 0; i < len; ++i)
{
auto pos = um.find(s[i]);
if (pos == um.end())
um.insert({ s[i], t[i] });
else{
if ((*pos).second != t[i])
return false;
}//else
}//for //再验证字符串t—>s的映射
um.clear();
for (int i = 0; i < len; ++i)
{
auto pos = um.find(t[i]);
if (pos == um.end())
um.insert({ t[i], s[i] });
else{
if ((*pos).second != s[i])
return false;
}//else
}//for
return true;
}
};

GitHub测试程序源码

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

  1. LeetCode(43)Multiply Strings

    题目 Given two numbers represented as strings, return multiplication of the numbers as a string. Note: ...

  2. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  3. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  4. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  5. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  6. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  7. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  8. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  9. LeetCode(4)Median of Two Sorted Arrays

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

随机推荐

  1. c#基础_ArrayList

    list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }); //list.AddRange(list); //list.Clear();清空所有元素 ...

  2. sql常用操作(三)多表查询

    1 连接查询 1.1连接就是指两个或2个以上的表(数据源)“连接起来成为一个数据源”. 实际上,两个表的完全的连接是这样的一个过程: 左边的表的每一行,跟右边的表的每一行,两两互相“横向对接”后所得到 ...

  3. vue-cli3项目优化首页加载过慢的一些心得

    博主最近发现vue-cli3项目做完后,点击首页加载时间好久啊,一般都要3-5s.这样的加载时间博主自己都受不了,所以就有了这个随笔,将自己的一些研究心得分享给大家. 首先推荐大家下载一个webpac ...

  4. css对应中文字的英文名称

    中文名 英文名 Unicode Unicode 2 Mac OS 华文细黑 STHeiti Light [STXihei] \534E\6587\7EC6\9ED1 华文细黑 华文黑体 STHeiti ...

  5. 【TensorFlow入门完全指南】神经网络篇·循环神经网络(RNN)

    第一步仍然是导入库和数据集. ''' To classify images using a reccurent neural network, we consider every image row ...

  6. uva10129 PlayOnWords(并查集,欧拉回路)

    判断无向图是否存在欧拉回路,就是看度数为奇数的点有多少个,如果有两个,那么以那分别两个点为起点和终点,可以构造出一条欧拉回路,如果没有,就任意来,否则,欧拉回路不存在. 首先用并查集判断连通,然后统计 ...

  7. UVA11212 EditingaBook ( IDA*搜索)

    首先说说IDS,就DFS限定一个层数上限maxd,如果在maxd范围内没有找到解,就增加maxd,继续搜索. 当访问到当前结点u时,估计还要搜索h(u)层,如果h(u)+当前层数d>maxd的时 ...

  8. [OpenMP] 并行计算入门

    OpenMP并行计算入门 个人理解 OpenMP是一种通过共享内存并行系统的多处理器程序设计的编译处理方案,通过预编译指令告诉编译器哪些代码块需要被并行化,通过拷贝代码块实现并行程序.对于循环的并行化 ...

  9. [web开发] Vue + spring boot + echart 微博爬虫展示平台

    1.微博登录 2.爬取数据 3.mysql存储 4.pyechart本地展示 5.用vue搭建网站web展示 先放图: 1.微博登录 新浪微博的登录不是简单的post就能解决的,他的登录有加密,所以我 ...

  10. shell的切换

    从zsh切换到bash:在命令行输入bash即可 从bash切换到zsh:在命令行输入zsh即可