题目:
Given two arrays of strings a1 and a2 return a sorted array r in lexicographical order of the strings of a1 which are substrings of strings of a2.
例子:
Example 1:
a1 = ["arp", "live", "strong"]
a2 = ["lively", "alive", "harp", "sharp", "armstrong"]
returns ["arp", "live", "strong"]
Example 2:
a1 = ["tarp", "mice", "bull"]
a2 = ["lively", "alive", "harp", "sharp", "armstrong"]
returns []
要求:
要求实现function inArray(array1,array2){}
测试用例:
a2 = ["lively", "alive", "harp", "sharp", "armstrong"]
a1 = ["xyz", "live", "strong"]
Test.assertSimilar(inArray(a1, a2), ["live", "strong"])
a1 = ["live", "strong", "arp"]
Test.assertSimilar(inArray(a1, a2), ["arp", "live", "strong"])
a1 = ["tarp", "mice", "bull"]
Test.assertSimilar(inArray(a1, a2), [])
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
供参考:
应该是寻找最大子串问题吧,参考动态规划问题(2)——寻找最长公共子串
简单来说,返回a2中存在的a1字串,顺序为a1的顺序。
比如harp和sharp都有字串arp。lively和alive都有字串live,amstrong有字串strong。然后返回的顺序对应a1的顺序,所以返回["arp", "live", "strong"]