【Leetcode】【Easy】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.
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的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【LeetCode题意分析&解答】43. Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【LeetCode每天一题】Multiply Strings(字符串乘法)
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...
- 【leetcode刷题笔记】Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【题解】 Codeforces Edu44 F.Isomorphic Strings (字符串Hash)
题面戳我 Solution 我们按照每个字母出现的位置进行\(hash\),比如我们记录\(a\)的位置:我们就可以把位置表示为\(0101000111\)这种形式,然后进行字符串\(hash\) 每 ...
- 【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 ...
- 【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 ...
随机推荐
- php 判断字符串之间包含关系
之前常用stristr , strpos判断. 因为处理1000W * 1000W级别,循环就是漫长漫长... 在此,对stristr, strpos, explode判断字符串包含关系处理速度对比 ...
- Java对zip格式压缩和解压缩
Java对zip格式压缩和解压缩 通过使用java的相关类可以实现对文件或文件夹的压缩,以及对压缩文件的解压. 1.1 ZIP和GZIP的区别 gzip是一种文件压缩工具(或该压缩工具产生的压缩文件格 ...
- java1.8中ConcurrentHashMap
java1.8中的ConcurrentHashMap做了非常大的改动,整个数据结构都发生了变化,已经不存在segment了.所以要好好重新查看下源码.这篇博客是逐步更行的,看一点写一点. 首先看一个很 ...
- redis数据类型(五)set类型
一. set类型 set是无序集合,最大可以包含(2 的 32 次方-1)个元素. set 的是通过 hash table 实现的,所以添加,删除,查找的复杂度都是 O(1). hash table ...
- oracle 数据库添加Java方法
create or replace and compile java source named "Bitconverter" aspublic class Bitconverter ...
- android_serialport_api代码分析
1. 导入Android studio android_serialport_api是一个开源的串口测试工具,代码应该是用eclipse工程(不确定,没用过eclipse,反正不是Android st ...
- js判断向量叉点 并求出交点坐标
代码如下可以直接运行,判断向量相交并求出交点坐标 <!DOCTYPE html> <html> <head> <meta http-equiv=" ...
- nodejs的gridfs基本操作
var mongoose = require('mongoose'); var Schema = mongoose.Schema; mongoose.connect('mongodb://127.0. ...
- android inflate压力泵,将视图发生整合的过程
转自:https://blog.csdn.net/u012702547/article/details/52628453?utm_source=copy inflate方法从大范围来看,分两种,三个参 ...
- [PHP] 从PHP 5.6.x 移植到 PHP 7.0.x新特性
从PHP 5.6.x 移植到 PHP 7.0.x 新特性: 1.标量类型声明 字符串(string), 整数 (int), 浮点数 (float), 布尔值 (bool),callable,array ...