资讯专栏INFORMATION COLUMN

[LintCode] String Homomorphism

tanglijun / 1482人阅读

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 characters. No two characters may map to the same character but a character may map to itself.

Notice

You may assume both s and t have the same length.

Example

Given s = "egg", t = "add", return true.

Given s = "foo", t = "bar", return false.

Given s = "paper", t = "title", return true.

Solution
public class Solution {
    /*
     * @param s: a string
     * @param t: a string
     * @return: true if the characters in s can be replaced to get t or false
     */
    public boolean isIsomorphic(String s, String t) {
        //Map => (k, v) = (frequency, amount of chars)
        int[] map_S = new int[256];
        int[] map_T = new int[256];
        if (s == null || t == null || s.length() != t.length()) return false;
        for (int i = 0; i < s.length(); i++) {
            map_S[(int)s.charAt(i)]++; 
            map_T[(int)t.charAt(i)]++;
        }
        Map map = new HashMap<>();
        for (int i = 0; i < 256; i++) {
            if (!map.containsKey(map_S[i])) map.put(map_S[i], 1);
            else map.put(map_S[i], map.get(map_S[i])+1);
        }
        for (int i = 0; i < 256; i++) {
            if (!map.containsKey(map_T[i])) return false;
            else map.put(map_T[i], map.get(map_T[i])-1);
            if (map.get(map_T[i]) < 0) return false;
        }
        return true;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/68453.html

相关文章

  • [LeetCode/LintCode] Top K Frequent Words

    LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...

    0x584a 评论0 收藏0
  • [LintCode/LeetCode] Word Break

    Problem Given a string s and a dictionary of words dict, determine if s can be break into a space-separated sequence of one or more dictionary words. Example Given s = lintcode, dict = [lint, code]. R...

    dunizb 评论0 收藏0
  • [LintCode] Missing String

    Problem Given two strings, you have to find the missing string. Example Given a string str1 = This is an exampleGiven another string str2 = is example Return [This, an] Solution public class Solution ...

    IamDLY 评论0 收藏0
  • [LintCode] String Compression

    String Compression Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2b1c5a3. If the compressed string w...

    Cruise_Chan 评论0 收藏0
  • [LintCode] Split String

    Problem Give a string, you can choose to split the string after one character or two adjacent characters, and make the string to be composed of only one character or two characters. Output all possibl...

    Riddler 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<