前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Baozi Training Leetcode Solution 205: Isomorphic Strings

Baozi Training Leetcode Solution 205: Isomorphic Strings

作者头像
包子面试培训
发布2019-07-12 15:20:26
4190
发布2019-07-12 15:20:26
举报
文章被收录于专栏:包子铺里聊IT

Blogger: https://blog.baozitraining.org/2019/07/leetcode-solution-205-isomorphic-strings.html

博客园:https://www.cnblogs.com/baozitraining/p/11112125.html

Problem Statement

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.

Example 1:

代码语言:javascript
复制
Input: s = "egg", t = "add"
Output: true

Example 2:

代码语言:javascript
复制
Input: s = "foo", t = "bar"
Output: false

Example 3:

代码语言:javascript
复制
Input: s = "paper", t = "title"
Output: true

Note:

You may assume both s and t have the same length.

Problem link

Video Tutorial

You can find the detailed video tutorial here

  • Youtube
  • B站

Thought Process

A relatively straightforward problem, very similar to Word Pattern. All we have to do is check the one to one mapping from string a to string b, also it needs to maintain a bijection mapping (meaning no two different characters in a should map to the same character in b)

Use bijection mapping

Check character one by one from a and b. If char in a hasn't been seen before, create a one to one mapping between this char in a and the char in b so later if this char in a is seen again, it has to map to b, else we return false. Moreover, need to make sure the char in b is never mapped by a different character.

Solutions

代码语言:javascript
复制
 1 public boolean isIsomorphic(String a, String b) {
 2         if (a == null || b == null || a.length() != b.length()) {
 3             return false;
 4         }
 5         Map<Character, Character> lookup = new HashMap<>();
 6         Set<Character> dupSet = new HashSet<>();
 7 
 8         for (int i = 0; i < a.length(); i++) {
 9             char c1 = a.charAt(i);
10             char c2 = b.charAt(i);
11 
12             if (lookup.containsKey(c1)) {
13                 if (c2 != lookup.get(c1)) {
14                     return false;
15                 }
16             } else {
17                 lookup.put(c1, c2);
18                 // this to prevent different c1s map to the same c2, it has to be a bijection mapping
19                 if (dupSet.contains(c2)) {
20                     return false;
21                 }
22                 dupSet.add(c2);
23             }
24         }
25         return true;
26     }

Time Complexity: O(N), N is the length of string a or string b

Space Complexity: O(N), N is the length of string a or string b because the hashmap and set we use

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-07-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 包子铺里聊IT 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Blogger: https://blog.baozitraining.org/2019/07/leetcode-solution-205-isomorphic-strings.html
  • Problem Statement
  • Video Tutorial
  • Thought Process
    • Use bijection mapping
    • Solutions
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档