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. 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 They have the same length.

I had no idea how to solve this problem at first, but it became very simple after I figured it out.

Approach:

Use two hashes to map each other:

sourceMap[t[x]] = s[x]

targetMap[ s[x] ] = t[x]

public class Solution { public boolean isIsomorphic(String s, String t) { if(s.length() != t.length()) return false; HashMap sh = new HashMap(); HashMap th = new HashMap(); for(int i=0;i 

This siteOriginal articleAll follow "Attribution-NonCommercial-ShareAlike 4.0 License (CC BY-NC-SA 4.0)Please retain the following annotations when sharing or adapting:

Original author:Jake Tao,source:「LeetCode – 205. Isomorphic Strings」

140
0 0 140

Further Reading

Post a reply

Log inYou can only comment after that.
Share this page
Back to top