leetcode 205

题目描述:

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.

解法一:

这是一个很好想的解法,比较好想,但是效率不高,就是一边遍历一边将元素插入map,这样要同时建立s到t的map和t到s的map,同时还要检查不一致,如果出现不一致,则返回false,否则返回true。

bool isIsomorphic(string s, string t)
{
    map<char, char> s_map;
    map<char, char> t_map;

    for(int i = 0; i < s.size(); i ++)
    {
        if(s_map.find(s[i]) != s_map.end() && s_map.find(s[i])->second != t[i])
            return false;
        else
            s_map[s[i]] = t[i];
        if(t_map.find(t[i]) != t_map.end() && t_map.find(t[i])->second != s[i])
            return false;
        else
            t_map[t[i]] = s[i];
    }
    return true;
}

解法二:

这个解法是我查看其他人的博客看到的,先简单介绍一下算法的思想,这个算法先建立一个对照表,因为char型字符总计128个,所以不用map,也不用unordered_map,而是用一个长度为128的数组来存储对照表,这个对照表仅仅和字符串中字符的位置有关,通过这样的对照表将s和t分别映射到一个新的字符串,满足题目条件的两个字符串应该是在映射后应该相等。

bool isIsomorphic(string s, string t)
{
    if(transferStr(s) == transferStr(t))
        return true;
    return false;
}

string transferStr(string s)
{
    char temp = ‘0‘;
    vector<char> table(128, 0);
    for(int i = 0; i != s.size(); i ++)
    {
        if(table[s[i]] == 0)
            table[s[i]] = temp ++;
        s[i] = table[s[i]];
    }

    return s;
}
时间: 2024-08-24 17:46:21

leetcode 205的相关文章

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",返回真. 批

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.

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.

(easy)LeetCode 205.Reverse Linked List

Reverse a singly linked list. 解法一:记录单链表每个节点的val,然后重新为单链表赋值.(取巧,仅仅是将val部分改变,原始node节点并没有改变) 代码如下: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution

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