Sign in

[Java][TwoPoints][LeetCode] Longest Word in Dictionary through Deleting #524

Given a string s and a string array dictionary, return the longest string in the dictionary that can be formed by deleting some of the given string characters. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.

Example 1:

Input: s = "abpcplea", dictionary = ["ale","apple","monkey","plea"]
Output: "apple"

Because return the longest word with the smallest lexicographical order. So, I need sort dictionary first. Each of two arrays have a pointer. Base on dictionary array and check s string one by one to find the same as char of dictionary. Record the string of dictionary, if satisfaction condition. I compare and get the longest word of dictionary, then return it.

因為同樣長度的字典要輸出字典排序最前面的字串,所以先針對字典的樹組做排序,兩個數組分別各自一個指針,以字典的數組為主,逐一檢查在s字串中是否都有字典中單一字串的字元。記錄滿足條件下的字典字串,同時比較滿足條件的字串長度,更新最長字串的字串作為最後輸出。