题目

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. GUI的最终选择 Tkinter(九):事件

    Tkinter事件处理 Tkinter应用会花费大部分的时间在处理事件循环中(通过mainloop()方法进入),事件可以是触发的鼠标,键盘的操作,管理窗口触发的重绘事件(在多数情况下都是有用户间接引 ...

  2. 如何更改Linux yum源?

    centos下可以通过yum很方便快捷的安装所需的软件和库,如果yum的源不好,安装速度会非常慢,centos默认官方源似乎都是国外的,所以速度无法保证,我一直使用163的源,感觉速度不错.下面就说说 ...

  3. JavaScript笔记5-事件

    一.概述: 事件是可以被JavaScript侦测到的行为.网页中的每个元素都可以产生某些可以触发JavaScript函数的事件.相当于让标签在满足某种条件的时候,调用指定的方法. 二.常用事件 1:o ...

  4. Properties-转换流-打印流-序列化和反序列化-Commons-IO工具类

    一.Properties 类(java.util)     概述:Properties 是一个双列集合;Properties 属于map的特殊的孙子类;Properties 类没有泛型,propert ...

  5. 枚举转List

    将枚举值转为list (name,value) 的形式 /// <summary> /// 获取口味 /// </summary> /// <returns>< ...

  6. Jquery 事件 DOM操作

    常规事件: 把JS的事件  on去掉即可 例如:js    document.getElementById("id").onclinck=function(){} Jquery   ...

  7. WinForm 窗体API移动 API阴影

    窗体移动 //窗体移动API [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllI ...

  8. HDU 3681 Prison Break 越狱(状压DP,变形)

    题意: 给一个n*m的矩阵,每个格子中有一个大写字母,一个机器人从‘F’出发,拾取所有的开关‘Y’时便能够越狱,但是每走一格需要花费1点能量,部分格子为充电站‘G’,每个电站只能充1次电.而且部分格子 ...

  9. Page_Load与sender -- PostBack是由哪个 ASP.NET控件引起 ?

    Page_Load与sender -- PostBack是由哪个 ASP.NET控件引起 ? 之前有讨论过ASP.NET Web Form "事件"里面的 "sender ...

  10. BZOJ 1396:识别子串 SA+树状数组+单调队列

    1396: 识别子串 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 381  Solved: 243[Submit][Status][Discuss] ...