前往
大廳
主題

LeetCode - 1662. Check If Two String Arrays are Equivalent 解題心得

Not In My Back Yard | 2023-09-16 12:00:21 | 巴幣 0 | 人氣 120

題目連結:


題目意譯:
給定兩字串陣列 word1 和 word2,如果兩陣列代表著相同字串則回傳真(True);反之,回傳假(False)。

一陣列「代表著」一字串,代表著該陣列中的元素按照順序串接在一起將會形成該字串。

限制:
1 ≦ word1.length, word2.length ≦ 10 ^ 3
1 ≦ word1[i].length, word2[i].length ≦ 10 ^ 3
1 ≦ sum(word1[i].length), sum(word2[i].length) ≦ 10 ^ 3
word1[i] 和 word2[i] 由小寫英文字母組成。



範例測資:
範例 1:
輸入: word1 = ["ab", "c"], word2 = ["a", "bc"]
輸出: true
解釋:
word1 代表字串 "ab" + "c" → "abc"
word1 代表字串 "a" + "bc" → "abc"
兩字串相同,因此我們回傳真。

範例 2:
輸入: word1 = ["a", "cb"], word2 = ["ab", "c"]
輸出: false

範例 3:
輸入: word1  = ["abc", "d", "defg"], word2 = ["abcddefg"]
輸出: true


解題思維:
因為兩陣列代表的字串都不會太長(最長只到 10 ^ 3 個字元),因此我們直接各自串接起來找到各自代表的字串並比較有沒有相同即可。




此次分享到此為止,如有任何更加簡潔的想法或是有說明不清楚之地方,也煩請各位大大撥冗討論。

創作回應

更多創作