题目

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. 什么是微服务架构,.netCore微服务选型

    什么是微服务架构,.netCore微服务选型 https://www.cnblogs.com/uglyman/p/9182485.html 开发工具:VS2017 .Net Core 2.1 什么是微 ...

  2. 1550: Simple String 最大流解法

    http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1550 很久以前做的一题,当时队友用最大流做,现在我也是 这个转化为二分图多重匹配,就是一样的意 ...

  3. SQL生成日期维度(到小时)

    #建表语句: CREATE TABLE [dbo].[Dim_日期3]( ) NOT NULL, [年] [int] NULL, ) NULL, ) NULL, ) NULL, ) NULL, ) N ...

  4. 基于spring-boot和docker-java实现对docker容器的动态管理和监控[附完整源码下载]

    ​ (我是个封面) docker简介 Docker 是一个开源的应用容器引擎,和传统的虚拟机技术相比,Docker 容器性能开销极低,因此也广受开发者喜爱.随着基于docker的开发者越来越多,doc ...

  5. JSP jsp内置对象

    jsp(java server pages):java服务器端的页面   JSP的执行过程 1.浏览器输入一个jsp页面 2.tomcat会接受*.jsp请求,将该请求发送到org.apache.ja ...

  6. java 通过文件后缀名查找文件

    最近开发项目的时候需要过滤出一些指定的文件,所以有了以下的一些代码: /** **该类主要是过滤得到指定后缀名的文件 **/ public class DataFileFilter implement ...

  7. PostgreSQL数据类型

    http://blog.csdn.net/neo_liu0000/article/category/797059 第六章  数据类型 6.1概述 PostgreSQL 提供了丰富的数据类型.用户可以使 ...

  8. 虚拟机VMware Workstation Pro装Mac遇到的一些问题【总结】

    1. 问题:VM找不到Apple Mac X(M)? 解决方法:在网上找unlocker20*下载: 电脑装一个python3版本以下的版本(装python,主要是编译.因为下载的插件是python写 ...

  9. self & this 上下文

    对象:指向对象的首地址: 函数:代表了函数运行的主要上下文: 内部:在类的内部使用. self Within the body of a class method, self refers to th ...

  10. netbackup如何手动获取主机ID证书。

    如何手动获取主机ID证书.   文章:100039650 最后发布:2017-09-21 评分:  20 11 产品:NetBackup 问题 从NetBackup V8.1开始,管理员需要在证书颁发 ...