Leetcode 205:isomorphic Strings

Source: Internet
Author: User


Given strings sand T, determine if they is isomorphic.

The strings is isomorphic if the characters in s can is replaced to gett.

All occurrences of a character must is replaced with another character while preserving the order of characters. No, characters may map to the same character and a character may map to itself.

For example,
Given "egg" , "add" return True.

Given "foo" , "bar" return FALSE.

Given "paper" , "title" return True.

Note:
Assume both s and T have the same length.

Ideas

Create a map to save the mapping relationship, while maintaining the mapped char with a set, ensuring that the same char is not mapped two times.

[CODE]

public class Solution {    //test case: ' Egg ', ' Add ' public        Boolean isisomorphic (string s, String t) {        //init che CK        if (s==null | | t==null) return false;        if (s.length () = T.length ()) return false;                Map<character, character> map = new Hashmap<character, character> ();        set<character> set = new Hashset<character> ();                for (int i=0; i<s.length (); i++) {            char C1 = S.charat (i);            char C2 = T.charat (i);                        if (Map.containskey (C1)) {                if (Map.get (c1)! = C2) return false;            } else {                if (set.contains (C2)) return False ;                else {                    map.put (c1, C2);                    Set.add (C2);        }}} return true;    }}


Leetcode 205:isomorphic 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.