<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)205:同構(gòu)字符串

          共 1604字,需瀏覽 4分鐘

           ·

          2021-03-14 14:07

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做 同構(gòu)字符串,我們先來看題面:
          https://leetcode-cn.com/problems/isomorphic-strings/

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

          Two strings s and t 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.

          題意

          給定兩個(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


          解題

          判斷每個(gè)字符的索引相同,如果后面有重復(fù)的字符,index就會(huì)直接索引到第一次出現(xiàn)的位置。相當(dāng)于同一字符第一個(gè)出現(xiàn)的位置映射到字符串中所有相同的字符。


          class Solution:
              def isIsomorphic(self, s: str, t: str) -> bool:
                  for i in range(len(s)):
                      if s.index(s[i]) != t.index(t[i]):
                          return False
                  return True


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力 。

          上期推文:

          LeetCode1-200題匯總,希望對你有點(diǎn)幫助!

          LeetCode刷題實(shí)戰(zhàn)201:數(shù)字范圍按位與

          LeetCode刷題實(shí)戰(zhàn)202:快樂數(shù)

          LeetCode刷題實(shí)戰(zhàn)203:移除鏈表元素

          LeetCode刷題實(shí)戰(zhàn)204:計(jì)數(shù)質(zhì)數(shù)


          瀏覽 30
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  可以看的欧美激情网站 | 最新亚洲视频免费在线观看 | 国产91乱码一区二区三区 | 欧美搞搞网 | 天天综合网7799精品视频 |