LintCode之Strings Homomorphism

判断两个字符串的结构是否相同。

样例:
Given s = “egg”, t = “add”, return true.
Given s = “foo”, t = “bar”, return false.
Given s = “paper”, t = “title”, return true.

public static boolean isIsomorphic(String s, String t) {

        String str1 = stringStructure(s);
        String str2 = stringStructure(t);

        if (str1.equals(str2)) {
            return true;
        }
        return false;
    }

    public static String stringStructure(String s) {

        List<String> list = new ArrayList<>();
        for (int i = 0; i < s.length(); i++) {
            if (!list.contains(s.charAt(i) + "")) {
                list.add(s.charAt(i) + "");
            }
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            for (int j = 0; j < list.size(); j++) {
                if ((s.charAt(i)+"").equals(list.get(j))) {
                    sb.append(j);
                }
            }
        }
        return sb+"";
    }

你可能感兴趣的:(LintCode)