isIsomorphic

超时版:

/*
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));

    }

}
时间: 2024-12-21 07:39:43

isIsomorphic的相关文章

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 chara

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--easy系列10

#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 t

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

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

Isomorphic Strings leetcode

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.