码迷,mamicode.com
首页 > 其他好文 > 详细

isIsomorphic

时间:2015-06-03 00:54:17      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

超时版:

/*
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.*/
import java.util.*;

public class Isomorphic_strings {

    public static void main(String[] args)
    // TODO Auto-generated method stub
    {
        System.out.println(isIsomorphic("papera", "titlei"));
        /*
         * String str="dauqka"; String str1=str.replace(‘a‘,‘x‘); str=str1;
         * System.out.println(str);
         */

    }

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

        int x = 0;
        if (s.length() != t.length())
            return false;
        char[] s_chs = s.toCharArray();
        char[] t_chs = t.toCharArray();
        for (int i = 0; i < s_chs.length; i++) {
            if (!(s_chs[i] == t_chs[i])) {
                for (int j = 0; j < i; j++) {
                    if ((t_chs[j] == t_chs[i]))
                        x = 1;
                }
            }
            if (x == 0)
                t = t.replace(t_chs[i], s_chs[i]);
            if (x == 1) {
                t_chs[i] = s_chs[i];
                t = String.valueOf(t_chs);

            }
            t_chs = t.toCharArray();
        }

        return (s.equals(t));

    }

}

 

isIsomorphic

标签:

原文地址:http://www.cnblogs.com/kydnn/p/4548109.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!