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

Subscribe to see which companies asked this question

解题分析:

这里的意思和前几天刷的一道题目很像。就是判断字符串的结构,这里可以说判断构成,

举例:

egg 与 add, 这里存储 e 对应着 a,  g 对应着 d.然后再判断的时候发现有问题,对应不上,说明就不是同构的。

# -*- coding:utf-8 -*-
__author__ = 'jiuzhang'
class Solution(object):
    def isIsomorphic(self, s, t):
        s_list = list(s)
        t_list = list(t)
        if len(s) != len(t):
            return False
        sDict, tDict = {}, {}
        for i, j in zip(s_list, t_list):
            if i not in sDict:
                sDict[i] = j
            if j not in tDict:
                tDict[j] = i
            if tDict[j] != i or sDict[i] != j:
                return False
        return True
时间: 2024-08-06 12:19:58

(LeetCode)Isomorphic Strings --- 同构字符串的相关文章

[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>

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

[LeetCode]51. Ismorphic 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.

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的素数个

【LeetCode-面试算法经典-Java实现】【205-Isomorphic Strings(同构字符串)】

[205-Isomorphic Strings(同构字符串)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 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