leetcode 205. 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.

题解:显然是哈希查找,但是其实没必要用map,一共就255个字符,每次只记录它现在的位置就好(因为之前的位置都比较过了)。

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        int hs[256]={0};
        int ht[256]={0};
        int ls=s.length(),lt=t.length();
        if(ls!=lt) return false;
        for(int i=0;i<ls;i++){
            if(hs[s[i]]!=ht[t[i]]){
                return false;
            }
            hs[s[i]]=i+1;
            ht[t[i]]=i+1;
        }
        return true;
    }
};
时间: 2024-12-19 08:08:14

leetcode 205. 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 205 Isomorphic Strings

Problem: 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 cha

[LeetCode] 205. Isomorphic Strings 解题思路 - Java

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.

Java [Leetcode 205]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 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)

翻译 给定两个字符串s和t,决定它们是否是同构的. 如果s中的元素被替换可以得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换另一个字符串的元素的过程中,所有字符的顺序必须保留. 没有两个字符可以被映射到相同的字符,但字符可以映射到该字符本身. 例如, 给定"egg","add",返回真. 给定"foo","bar",返回假. 给定"paper","title",返回真. 批

Java for LeetCode 205 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: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

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题解——数据结构之哈希表

1. 数组中两个数的和为给定值 2. 判断数组是否含有重复元素 3. 最长和谐序列 4. 最长连续序列 哈希表使用 O(N) 空间复杂度存储数据,并且以 O(1) 时间复杂度求解问题. Java 中的 HashSet 用于存储一个集合,可以查找元素是否在集合中.如果元素有穷,并且范围不大,那么可以用一个布尔数组来存储一个元素是否存在.例如对于只有小写字符的元素,就可以用一个长度为 26 的布尔数组来存储一个字符集合,使得空间复杂度降低为 O(1). Java 中的 HashMap 主要用于映射关