publicint(String S, String[] words){ List<List<Integer>> pos = new ArrayList<>(); int result = 0;
for (int i = 0; i < 26; i++) pos.add(new ArrayList<>());
for (int i = 0; i < S.length(); i++) pos.get(S.charAt(i) - 'a').add(i);
for (String word : words) { if (match(pos, word)) result++; }
return result; }
privatebooleanmatch(List<List<Integer>> pos, String word){ int prev = -1;
for (char ch : word.toCharArray()) { List<Integer> list = pos.get(ch - 'a'); int current = Collections.binarySearch(list, prev + 1); if (current < 0) current = - current - 1; if (current >= list.size()) returnfalse;
近期评论