【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 "foo", "app"; returns true we can map f -> a and o->p
given "bar", "foo"; returns false we can‘t map both ‘a‘ and ‘r‘ to ‘o‘

given "ab", "ca"; returns true we can map ‘a‘ -> ‘b‘ and ‘c‘ -> ‘a‘

Solution 1: map

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        if(s.size()!=t.size())return false;
        map<char, char> m;
        for(int i=0;i<s.size();i++){
            if(!m.count(s[i])){
                map<char,char>::const_iterator iter=m.begin();
                while(iter!=m.end()){
                    if(iter->second==t[i])return false;
                    iter++;
                }
                m.insert(make_pair(s[i], t[i]));
            }else{
                if(m[s[i]]!=t[i])return false;
            }
        }
        return true;
    }
};

Solution 2:

bool isIsomorphic(string s, string t) {
if(s.size()!=t.size()) return false;
map<char,char> map1;
map<char,char> map2;

for(int i=0;i<s.length();i++){
char c1=s[i];
char c2=t[i];
if(map1.count(c1)){
if(map1[c1]!=c2) return false;
}
if(map2.count(c2)){
if(map2[c2]!=c1) return false;
}

map1.insert(c1, c2);
map2.insert(c2, c1);
}
return true;
}

时间: 2024-08-06 15:41:12

【LeetCode】205 - Isomorphic Strings的相关文章

【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 character

LeetCode OJ 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】893. Groups of Special-Equivalent Strings

Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-of-special-equivalent-strings/ 1)problem You are given an array A of strings. Two strings S and T are special-equivalent if after any number of moves,

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】哈希表 hash_table(共88题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target . 题解:我这次最大范围的优化代码, hash-table + one pass,时间复杂度 O(N),空间复杂度 O(N).重点在于动态找,一边生成hash-tabl

【LeetCode】Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". public class Solution { public String addBinary(String a, String b) { if(a.equalsIgnoreCase("")||a==null) r

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->