LeetCode Isomorphic Strings 对称字符串

题意:如果两个字符串是对称的,就返回true。对称就是将串1中的同一字符都一起换掉,可以换成同串2一样的。

思路:ASCII码表哈希就行了。需要扫3次字符串,共3*n的计算量。复杂度O(n)。从串左开始扫,若字符没有出现过,则赋予其一个特定编号,在哈希表中记录,并将该字符改成编号。对串2同样处理。其实扫2次就行了,但是为了短码。

 1 class Solution {
 2 public:
 3     void cal(string &s)
 4     {
 5         int has[129]={0}, cnt=0;
 6         for(int i=0; i<s.size(); i++)
 7         {
 8             if(!has[s[i]])
 9                 has[s[i]]=++cnt;
10             s[i]=has[s[i]];
11         }
12     }
13
14     bool isIsomorphic(string s, string t) {
15         if(s.size()!=t.size())    return false;
16         if(s.size()==1)    return true;
17         cal(s);
18         cal(t);
19         for(int i=0; i<s.size(); i++)
20             if(s[i]!=t[i])    return false;
21         return true;
22     }
23 };

AC代码

时间: 2024-11-10 11:32:12

LeetCode Isomorphic Strings 对称字符串的相关文章

(LeetCode)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.

[leetcode]205. Isomorphic Strings同构字符串

哈希表可以用ASCII码数组来实现,可以更快 public boolean isIsomorphic(String s, String t) { /* 思路是记录下每个字符出现的位置,当有重复时,检查另外一个map是不是也是对应位置重复 */ if (s.length()!=t.length()) { return false; } Map<Character,Integer> map1 = new HashMap<>(); Map<Character,Integer>

Python3解leetcode 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 charac

[leetcode]Isomorphic Strings 解题报告 C语言

[题目] 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 charact

CodeForces985F:Isomorphic Strings (字符串&amp;hash)

题意:取出字符串Str里的两个串S,T,问对应位置的的字符在否有一一映射关系. hash:对于每个字符s=‘a’-‘z’,我们任意找一个i,满足Si==s,(代码里用lower_bound在区间找到最小的位置i)其对应的字符为Ti==t.然后我们判断这段区间里s的hash值是否等于t的hash值.不难证明26个字母都满足时对应hash相同时,才有一一映射关系.(但是不明白我的自然溢出的hash版本为什么错了,但是mod(1e9+7)的版本对了? #include<bits/stdc++.h>

LeetCode:Isomorphic Strings

1.题目名称 Isomorphic Strings(同构的字符串) 2.题目地址 https://leetcode.com/problems/isomorphic-strings/ 3.题目内容 英文: 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 occurre

leetcode 204/187/205 Count Primes/Repeated DNA Sequences/Isomorphic Strings

一:leetcode 204 Count Primes 题目: Description: Count the number of prime numbers less than a non-negative number, n 分析:此题的算法源码可以参看这里,http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes 代码: class Solution { public: int countPrimes(int n) { // 求小于一个数n的素数个

205. Isomorphic Strings - LeetCode

Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中相同位置字符的差是否相同 Java实现: public boolean isIsomorphic(String s, String t) { Map<String, Integer> map = new HashMap<>(); for (int i=0; i<s.length(

LeetCode: Multiply Strings [042]

[题目] Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. [题意] 给定用字符串表示的整数,返回两个数的乘积结果字符串.两个数字都非负,且能任意大. [思路] 1. 考虑其中一个数是0的情况 2. 模拟乘法运算过程 维护一个vecto