前往
大廳
主題

LeetCode - 2108. Find First Palindromic String in the Array 解題心得

Not In My Back Yard | 2022-06-10 12:00:01 | 巴幣 0 | 人氣 125

題目連結:


題目意譯:
給定一個字串陣列 words,回傳陣列中第一個迴文字串。如果沒有這樣子的字串,則回傳一個空字串 ""。

一字串如果從頭讀到尾或是從尾讀到頭是一樣的,則其為迴文。

限制:
1 ≦ words.length ≦ 100
1 ≦ words[i].length ≦ 100
words[i] 只由小寫英文字母組成。



範例測資:
範例 1:
輸入: words = ["abc","car","ada","racecar","cool"]
輸出: "ada"
解釋: The first string that is palindromic is "ada".
Note that "racecar" is also palindromic, but it is not the first.

範例 2:
輸入: words = ["notapalindrome","racecar"]
輸出: "racecar"
解釋: The first and only string that is palindromic is "racecar".

範例 3:
輸入: words = ["def","ghi"]
輸出: ""
解釋: There are no palindromic strings, so the empty string is returned.


解題思維:
依序掃過 words 中每個字串去判斷即可。至於單一字串的迴文判斷可以參見這題。如果掃完了之後找不到迴文字串,則此時即回傳 ""。




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

創作回應

更多創作