LeetCode(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.
分析
判断给定的两个字符串是否同构,字符串字面上很容易就能看得出来;
但是怎么用算法来判断呢?
仔细分析:
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;
}
};
LeetCode(205)Isomorphic Strings的更多相关文章
- LeetCode(43)Multiply Strings
题目 Given two numbers represented as strings, return multiplication of the numbers as a string. Note: ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- 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 ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- spark-2.2.0-bin-hadoop2.6和spark-1.6.1-bin-hadoop2.6发行包自带案例全面详解(java、python、r和scala)之Basic包下的JavaPageRank.java(图文详解)
不多说,直接上干货! spark-1.6.1-bin-hadoop2.6里Basic包下的JavaPageRank.java /* * Licensed to the Apache Software ...
- Jexus~docker与它产生了暖味
前段时间写了很多docker for .net core的文章,用来快速部署微服务相当给力,而尝到了香头的我们希望把.net frameworks的程序也使用docker来部署一下,那么接下来我就结果 ...
- Python 踩坑之旅进程篇其四一次性踩透 uid euid suid gid egid sgid的坑坑洼洼
目录 1.1 踩坑案例 1.2 填坑解法 1.3 坑位分析 1.4 技术关键字 1.5 坑后思考 下期坑位预告 代码示例支持 平台: Centos 6.3 Python: 2.7.14 代码示例: 菜 ...
- NOPI Excel 读取公式生成后的数据
using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using S ...
- node-amqp 使用fanout发布订阅rabbitmq消息
publisher代码 const amqp = require('amqp'); let option = { host: 'server-ip', port: 5672, login: 'gues ...
- UIScrollView使用stoboard自动布局
使用stoboard布局 scrollView 是有点麻烦的,首先我们往往约束好一个 scrollView 然后在添加子控件,此时都会报错,原因是, scrollView必须确定滚动范围 然后在使用V ...
- 【Java】 hashcode()和System.identityHashCode()
hashcode()和System.identityHashCode() openjdk8: http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/5b86f ...
- 小米OJ刷题日志
虽然这OJ上的题比较水,但还是挺有意思的.关键是能赚钱 特别是提交方式 居然不支持C++,垃圾OJ 4. 最长连续数列 排序后dp 5. 找出旋转有序数列的中间值 写个排序就做完了. 6. 交叉队列 ...
- 《移动Web前端高效开发实战》笔记4--打造单页应用SPA
路由是一个单页应用的核心,大部分前端框架都实现了一个复杂的路由库,包括动态路由,路由钩子,组件生命周期甚至服务器端渲染等复杂的功能.但是对于前端开发者而言,路由组件的核心是URL路径到函数的映射,了解 ...
- ABAP数据类型
数据类型表: 类型缩写 类型 默认长度 允许长度 初始值 描述 C 文本型 1 Space 字符串数据,如'Program' D 日期型 8 8 '00000000' 日期数据,格式为YYYYMM ...