LeetCode OJ: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就可以解决,不过要注意双向都要检查,代码如下:

 1 class Solution {
 2 public:
 3     bool isIsomorphic(string s, string t) {
 4         map<char, char> m1;
 5         map<char, char> m2;
 6         if(s.size() != t.size())
 7             return false;
 8         for(int i = 0; i < s.size(); ++i){
 9             if(m1.find(s[i]) == m1.end() && m2.find(t[i]) == m2.end()){
10                 m1[s[i]] = t[i];
11                 m2[t[i]] = s[i];
12             }
13             else if(m1.find(s[i]) != m1.end() && m2.find(t[i]) != m2.end()){
14                 if(m1[s[i]] != t[i] || m2[t[i]] != s[i])
15                     return false;
16             }else{
17                 return false;
18             }
19         }
20         return true;
21     }
22 };
时间: 2024-10-15 15:30:45

LeetCode OJ: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]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.

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

LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)

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

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

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】Isomorphic Strings(easy)

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.