描述
给定一个字符串target和字符串数组s,按在s中的原次序输出s中所有包含target的字符串(即满足target为s[i]的一个子序列)
s.length<=10001<=s中所有字符串长度之和,target<=100000
样例
给定target="google",s=["goooogle","abc","google","higoogle","googlg","gowogwle","gogle"]
,返回["goooogle","google","higoogle","gowogwle"]
解释:
goooogle google higoogle gowogwleDescriptionGiven a word, you need to judge whether the usage of capitals in it is right or not.We define the usage of capitals in a word to be right when one of the following cases holds:All letters in this word are capitals, like "USA".All letters in this word are not capitals, like "leetcode".Only the first letter in this word is capital if it has more than one letter, like "Google".Otherwise, we define that this word doesn't use capitals in a right way.The input will be a non-empty word consisting of uppercase and lowercase latin letters.
public class Solution { /** * @param target: the target string * @param s: * @return: output all strings containing target in s */ public String[] getAns(String target, String[] s) { Listresult = new ArrayList<>(s.length); for (String str : s) { if (isSubsequence(target, str)) { result.add(str); } } return result.toArray(new String[0]); } // is s subsequence of t private boolean isSubsequence(String s, String t) { for (int i = 0, offset = 0; i < s.length(); i++, offset++) { offset = t.indexOf(s.charAt(i), offset); if (offset == -1) return false; } return true; }}