Leetcode#205isomorphic Strings

Source: Internet
Author: User

Given strings s and t, determine if they are isomorphic.

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

all occurrences of a character must be replaced with another character while preserving the order of characters. No. 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.

The topic analysis, two strings between the one by one mapping relationship, using hash to deal with, because it consists of two strings, so you must build two hash map table, to ensure two strings between the one by one correspondence between the characters

public class Solution {

public Boolean isisomorphic (string s, String t) {

Hashmap<character,character> map1=new Hashmap<character, character> ();

Hashmap<character,character> map2=new Hashmap<character, character> ();

for (int i=0;i<s.length (); i++)

{

if (Map1.get (S.charat (i)) ==null)

Map1.put (S.charat (i), T.charat (i));

if (Map2.get (T.charat (i)) ==null)

Map2.put (T.charat (i), S.charat (i));

if (Map1.get (S.charat (i))!=t.charat (i) | | Map2.get (T.charat (i))!=s.charat (i))

return false;

}

return true;

}

}


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