205-ismorphics strings

Source: Internet
Author: User

[Question]

Given two stringsSAndT, Determine if they are isomorphic.

Two strings are isomorphic if the characters inSCan be replaced to getT.

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.

Given two strings S and T, determine whether they are homogeneous.

If all characters in s can be replaced by characters in T, the two strings are homogeneous.

All the characters that appear must be replaced by another character. No two characters can be mapped to the same character.

Example

To "egg", "add", return true.

To "foo", "bar", return false.

To "paper", "title", return true.

[Analysis]

1. Map the characters of two strings to map

[Algorithm Implementation]

public class Solution {    public boolean isIsomorphic(String s, String t) {        Map<Character,Character> map = new HashMap<Character,Character>();        //Map<Character,Character> map2 = new HashMap<Character,Character>();        char[] cs = s.toCharArray();        char[] ts = t.toCharArray();        int len = cs.length;        for(int i=0; i<len; i++) {            if(map.containsKey(cs[i])) {                if(map.get(cs[i])!=ts[i])                    return false;            }else if(map.containsValue(ts[i])){                return false;            }else {                map.put(cs[i], ts[i]);            }        }        return true;    }}

 

205-ismorphics strings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.