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中相同位置的字符,所有出现的位置都相同。

如果能记录每个字符出现的位置,同时遍历两个字符串,当遍历到新的字符时,检查各自字符之前出现的位置,之前的位置不一致,则返回false,否则继续检查下一对字符。

对于记录位置和查找操作,最方便的是使用hash字典。字典中记录的是此字符上一次出现的位置。

代码为:

 class Solution {
public:
bool isIsomorphic(string s, string t) {
map<char, int> char_s;
map<char, int> char_t;
int len = s.size(); for (int i = ; i < len; ++i) {
if (!char_s.count(s[i]))
char_s[s[i]] = i;
if (!char_t.count(t[i]))
char_t[t[i]] = i;
if (char_s[s[i]] != char_t[t[i]])
return false;
char_s[s[i]] = i;
char_t[t[i]] = i;
} return true;
}
};

【Leetcode】【Easy】Isomorphic Strings的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【LeetCode题意分析&解答】43. Multiply Strings

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

  6. 【LeetCode每天一题】Multiply Strings(字符串乘法)

    Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...

  7. 【leetcode刷题笔记】Multiply Strings

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

  8. 【题解】 Codeforces Edu44 F.Isomorphic Strings (字符串Hash)

    题面戳我 Solution 我们按照每个字母出现的位置进行\(hash\),比如我们记录\(a\)的位置:我们就可以把位置表示为\(0101000111\)这种形式,然后进行字符串\(hash\) 每 ...

  9. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  10. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

随机推荐

  1. Jquery动画操作的stop()函数

    今天做一个点击动画时,遇到了当快速连续点击时,动画效果会乱,并不是我们想要达到的效果. 查询了一下,确认是动画累积的原因.网上搜了一下,发现jquery 的stop()函数刚好能解决. stop(cl ...

  2. javascript中的function 函数名(){} 和 函数名:function(){}有什么不同

    function functionName(){};这是定义一个函数 functionName:function(){};是设置一个对象的方法. 下面举一个例子: <html> <h ...

  3. 剑指offer(21-25)编程题

    栈的压入.弹出序列 从上往下打印二叉树 二叉搜索树的后序遍历序列 二叉树中和为某一值的路径 复杂链表的复制 21.输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假 ...

  4. MkDocs

    Overview MkDocs is a fast, simple and downright gorgeous static site generator that's geared towards ...

  5. 【转】检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为【经典】模式)。

    检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为[经典]模式). 我们将ASP.NET程序从IIS6移植到IIS7,可能运行提示以下错误: HTTP 错误 500.23 ...

  6. 通过开机广播(broadcast)通知应用

    1. 概念 开机的时候,系统会发送一则广播,所有有标记的应用(通过广播接收者)都会获取得到,然后可以通过广播接收者去处理一些事情,比如启动该应用,或者处理数据: 代码:https://github.c ...

  7. shell脚本分析apache日志状态码

    一.首先将apache日志按天切割 vi /etc/httpd/conf/httpd.conf        ErrorLog "|rotatelogs /var/log/httpd/%Y% ...

  8. js控制div样式显示与隐藏,JS通过点击超链接右边(指定位置)显示一个图标

    原文出自:https://blog.csdn.net/seesun2012 javascript基础篇,老土的方法解决js控制div样式,便于新手理解,粗暴的不能再粗暴,如果你是高手,请忽略! 思路: ...

  9. 7. Reverse Integer 反向输出整数 easy 9. Palindrome Number 判断是否是水仙花数 easy

    Given a 32-bit signed integer, reverse digits of an integer. 将32位整数反向输出. Note:Assume we are dealing ...

  10. SQL索引优化及实战文章总结(总结)

    1. MySQL索引原理以及查询优化 2.