1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        ?LeetCode刷題實戰(zhàn)205:同構字符串

        共 1604字,需瀏覽 4分鐘

         ·

        2021-03-14 14:07

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

        今天和大家聊的問題叫做 同構字符串,我們先來看題面:
        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.

        題意

        給定兩個字符串 s 和 t,判斷它們是否是同構的。

        如果 s 中的字符可以按某種映射關系替換得到 t ,那么這兩個字符串是同構的。

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

        示例


        示例 1:

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

        示例 2

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

        示例 3

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


        解題

        判斷每個字符的索引相同,如果后面有重復的字符,index就會直接索引到第一次出現(xiàn)的位置。相當于同一字符第一個出現(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


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

        上期推文:

        LeetCode1-200題匯總,希望對你有點幫助!

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

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

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

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


        瀏覽 30
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            一区二区三区性爱视频 | 精品九九九九九 | 李小冉三级未删减电影 | 日本少妇裸体做爰高潮片 | 粗大浓稠硕大噗嗤噗嗤 | 娇妻张妍交换高潮 | 狠久久| 美女大逼逼 | 一区二区三区伦理电影 | 99色在线视频 |