自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

數(shù)據(jù)結(jié)構(gòu)與算法之同構(gòu)字符串

開(kāi)發(fā) 前端 算法
每個(gè)出現(xiàn)的字符都應(yīng)當(dāng)映射到另一個(gè)字符,同時(shí)不改變字符的順序。不同字符不能映射到同一個(gè)字符上,相同字符只能映射到同一個(gè)字符上,字符可以映射到自己本身。

[[441407]]

 同構(gòu)字符串

力扣題目鏈接:https://leetcode-cn.com/problems/isomorphic-strings

給定兩個(gè)字符串 s 和 t,判斷它們是否是同構(gòu)的。

如果 s 中的字符可以按某種映射關(guān)系替換得到 t ,那么這兩個(gè)字符串是同構(gòu)的。

每個(gè)出現(xiàn)的字符都應(yīng)當(dāng)映射到另一個(gè)字符,同時(shí)不改變字符的順序。不同字符不能映射到同一個(gè)字符上,相同字符只能映射到同一個(gè)字符上,字符可以映射到自己本身。

示例 1:

  • 輸入:s = "egg", t = "add"
  • 輸出:true

示例 2:

  • 輸入:s = "foo", t = "bar"
  • 輸出:false

示例 3:

  • 輸入:s = "paper", t = "title"
  • 輸出:true

提示:可以假設(shè) s 和 t 長(zhǎng)度相同。

思路

字符串沒(méi)有說(shuō)都是小寫(xiě)字母之類的,所以用數(shù)組不合適了,用map來(lái)做映射。

使用兩個(gè)map 保存 s[i] 到 t[j] 和 t[j] 到 s[i] 的映射關(guān)系,如果發(fā)現(xiàn)對(duì)應(yīng)不上,立刻返回 false

C++代碼 如下:

  1. class Solution { 
  2. public
  3.     bool isIsomorphic(string s, string t) { 
  4.         unordered_map<charchar> map1; 
  5.         unordered_map<charchar> map2; 
  6.         for (int i = 0, j = 0; i < s.size(); i++, j++) { 
  7.             if (map1.find(s[i]) == map1.end()) { // map1保存s[i] 到 t[j]的映射 
  8.                 map1[s[i]] = t[j]; 
  9.             } 
  10.             if (map2.find(t[j]) == map2.end()) { // map2保存t[j] 到 s[i]的映射 
  11.                 map2[t[j]] = s[i]; 
  12.             } 
  13.             // 發(fā)現(xiàn)映射 對(duì)應(yīng)不上,立刻返回false 
  14.             if (map1[s[i]] != t[j] || map2[t[j]] != s[i]) { 
  15.                 return false
  16.             } 
  17.         } 
  18.         return true
  19.     } 
  20. }; 

其他語(yǔ)言版本

Java

  1. class Solution { 
  2.     public boolean isIsomorphic(String s, String t) { 
  3.         Map<CharacterCharacter> map1 = new HashMap<>(); 
  4.         Map<CharacterCharacter> map2 = new HashMap<>(); 
  5.         for (int i = 0, j = 0; i < s.length(); i++, j++) { 
  6.             if (!map1.containsKey(s.charAt(i))) { 
  7.                 map1.put(s.charAt(i), t.charAt(j)); // map1保存 s[i] 到 t[j]的映射 
  8.             } 
  9.             if (!map2.containsKey(t.charAt(j))) { 
  10.                 map2.put(t.charAt(j), s.charAt(i)); // map2保存 t[j] 到 s[i]的映射 
  11.             } 
  12.             // 無(wú)法映射,返回 false 
  13.             if (map1.get(s.charAt(i)) != t.charAt(j) || map2.get(t.charAt(j)) != s.charAt(i)) { 
  14.                 return false
  15.             } 
  16.         } 
  17.         return true
  18.     } 

Python

  1. class Solution: 
  2.     def isIsomorphic(self, s: str, t: str) -> bool: 
  3.         default_dict1 = defaultdict(str) 
  4.         default_dict2 = defaultdict(str) 
  5.  
  6.         if len(s) != len(t): return false 
  7.  
  8.         for i in range(len(s)): 
  9.             if not default_dict1[s[i]]: 
  10.                 default_dict1[s[i]] = t[i] 
  11.  
  12.             if not default_dict2[t[i]]: 
  13.                 default_dict2[t[i]] = s[i] 
  14.  
  15.             if default_dict1[s[i]] != t[i] or default_dict2[t[i]] != s[i]: 
  16.                 return False 
  17.  
  18.         return True 

Go

  1. func isIsomorphic(s string, t string) bool { 
  2.  map1 := make(map[byte]byte) 
  3.  map2 := make(map[byte]byte) 
  4.  for i := range s { 
  5.   if _, ok := map1[s[i]]; !ok { 
  6.    map1[s[i]] = t[i] // map1保存 s[i] 到 t[j]的映射 
  7.   } 
  8.   if _, ok := map2[t[i]]; !ok { 
  9.    map2[t[i]] = s[i] // map2保存 t[i] 到 s[j]的映射 
  10.   } 
  11.   // 無(wú)法映射,返回 false 
  12.   if (map1[s[i]] != t[i]) || (map2[t[i]] != s[i]) { 
  13.    return false 
  14.   } 
  15.  } 
  16.  return true 

JavaScript

  1. var isIsomorphic = function(s, t) { 
  2.     let len = s.length; 
  3.     if(len === 0) return true
  4.     let maps = new Map(); 
  5.     let mapt = new Map(); 
  6.     for(let i = 0, j = 0; i < len; i++, j++){ 
  7.         if(!maps.has(s[i])){ 
  8.             maps.set(s[i],t[j]);// maps保存 s[i] 到 t[j]的映射 
  9.         } 
  10.         if(!mapt.has(t[j])){ 
  11.             mapt.set(t[j],s[i]);// mapt保存 t[j] 到 s[i]的映射 
  12.         } 
  13.         // 無(wú)法映射,返回 false 
  14.         if(maps.get(s[i]) !== t[j] || mapt.get(t[j]) !== s[i]){ 
  15.             return false
  16.         } 
  17.     }; 
  18.     return true
  19. }; 

 

責(zé)任編輯:姜華 來(lái)源: 代碼隨想錄
相關(guān)推薦

2021-12-24 11:59:47

數(shù)據(jù)結(jié)構(gòu)算法字符串

2021-12-23 14:09:43

數(shù)據(jù)結(jié)構(gòu)算法字符串

2019-03-07 15:43:22

Redis數(shù)據(jù)SDS

2020-10-30 09:56:59

Trie樹(shù)之美

2022-09-21 07:57:33

二叉搜索樹(shù)排序二叉樹(shù)

2022-09-26 07:56:53

AVL算法二叉樹(shù)

2020-12-31 05:31:01

數(shù)據(jù)結(jié)構(gòu)算法

2020-10-21 14:57:04

數(shù)據(jù)結(jié)構(gòu)算法圖形

2023-10-19 15:11:48

Redis

2023-03-08 08:03:09

數(shù)據(jù)結(jié)構(gòu)算法歸并排序

2020-10-20 08:14:08

算法與數(shù)據(jù)結(jié)構(gòu)

2020-10-12 11:48:31

算法與數(shù)據(jù)結(jié)構(gòu)

2022-01-18 19:13:52

背包問(wèn)題數(shù)據(jù)結(jié)構(gòu)算法

2023-10-27 07:04:20

2021-12-10 11:27:59

數(shù)據(jù)結(jié)構(gòu)算法單調(diào)遞增的數(shù)字

2009-08-11 14:43:42

C#數(shù)據(jù)結(jié)構(gòu)與算法

2021-12-08 11:31:43

數(shù)據(jù)結(jié)構(gòu)算法合并區(qū)間

2021-07-16 04:57:45

Go算法結(jié)構(gòu)

2009-08-11 14:51:11

C#數(shù)據(jù)結(jié)構(gòu)與算法

2023-03-07 08:02:07

數(shù)據(jù)結(jié)構(gòu)算法數(shù)列
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)