身份证、社保卡、网银……密码技术就在你身边
2026/5/12 2:07:39
给定字符串s和一个字符串数组words,返回words中是s的子序列的单词数目。
子序列:通过删除s中的一些字符(也可以不删除)而不改变剩余字符相对位置所形成的新字符串。
示例:
输入: s = "abcde", words = ["a","bb","acd","ace"] 输出: 3 解释: 有三个单词是s的子序列:"a","acd","ace"。暴力:
s中匹配方法:
s中的位置,然后对每个单词使用二分查找simportjava.util.*;classSolution{/** * 使用预处理和二分查找判断子序列 * * @param s 源字符串 * @param words 单词数组 * @return 是s的子序列的单词数目 */publicintnumMatchingSubseq(Strings,String[]words){// 1: 预处理 - 为每个字符记录其在s中出现的所有位置List<Integer>[]positions=newList[26];for(inti=0;i<26;i++){positions[i]=newArrayList<>();}for(inti=0;i<s.length();i++){positions[s.charAt(i)-'a'].add(i);}// 2: 使用缓存避免重复计算Map<String,Boolean>cache=newHashMap<>();intcount=0;// 3: 对每个单词判断是否为子序列for(Stringword:words){if(cache.containsKey(word)){if(cache.get(word)){count++;}continue;}booleanisSubseq=isSubsequence(word,positions);cache.put(word,isSubseq);if(isSubseq){count++;}}returncount;}/** * 使用二分查找判断单词是否为子序列 * * @param word 待检查的单词 * @param positions 字符位置预处理数组 * @return true表示是子序列,false表示不是 */privatebooleanisSubsequence(Stringword,List<Integer>[]positions){intprevIndex=-1;// 上一个匹配字符在s中的位置for(charc:word.toCharArray()){List<Integer>charPositions=positions[c-'a'];// 如果字符c在s中不存在,直接返回falseif(charPositions.isEmpty()){returnfalse;}// 二分查找第一个大于prevIndex的位置intleft=0,right=charPositions.size();while(left<right){intmid=left+(right-left)/2;if(charPositions.get(mid)<=prevIndex){left=mid+1;}else{right=mid;}}// 如果没有找到合适的位置if(left==charPositions.size()){returnfalse;}// 更新prevIndex为找到的位置prevIndex=charPositions.get(left);}returntrue;}}importjava.util.*;classSolution{/** * 使用多指针判断子序列 * 为每个单词维护一个指针,同时遍历s */publicintnumMatchingSubseq(Strings,String[]words){// 使用缓存避免重复计算Map<String,Integer>wordCount=newHashMap<>();for(Stringword:words){wordCount.put(word,wordCount.getOrDefault(word,0)+1);}// 为每个唯一单词创建指针Map<String,Integer>pointers=newHashMap<>();for(Stringword:wordCount.keySet()){pointers.put(word,0);}intmatchedCount=0;// 遍历s的每个字符for(charc:s.toCharArray()){// 复制需要更新的单词列表List<String>toRemove=newArrayList<>();// 检查每个单词的当前指针位置for(Stringword:pointers.keySet()){intptr=pointers.get(word);if(ptr<word.length()&&word.charAt(ptr)==c){ptr++;pointers.put(word,ptr);// 如果单词完全匹配if(ptr==word.length()){matchedCount+=wordCount.get(word);toRemove.add(word);}}}// 移除已完全匹配的单词for(Stringword:toRemove){pointers.remove(word);}}returnmatchedCount;}}importjava.util.*;classSolution{/** * 使用Collections.binarySearch优化的二分查找 */publicintnumMatchingSubseq(Strings,String[]words){// 预处理字符位置List<Integer>[]positions=newList[26];for(inti=0;i<26;i++){positions[i]=newArrayList<>();}for(inti=0;i<s.length();i++){positions[s.charAt(i)-'a'].add(i);}Map<String,Boolean>cache=newHashMap<>();intcount=0;for(Stringword:words){if(cache.computeIfAbsent(word,w->isSubsequenceOptimized(w,positions))){count++;}}returncount;}privatebooleanisSubsequenceOptimized(Stringword,List<Integer>[]positions){intprevIndex=-1;for(charc:word.toCharArray()){List<Integer>list=positions[c-'a'];if(list.isEmpty())returnfalse;// 使用Collections.binarySearch找到插入位置intpos=Collections.binarySearch(list,prevIndex+1);if(pos<0){pos=-pos-1;// 转换为插入位置}if(pos>=list.size()){returnfalse;}prevIndex=list.get(pos);}returntrue;}}importjava.util.*;classSolution{/** * 暴力双指针,使用缓存优化 */publicintnumMatchingSubseq(Strings,String[]words){Map<String,Boolean>cache=newHashMap<>();intcount=0;for(Stringword:words){if(cache.computeIfAbsent(word,w->isSubsequenceBrute(s,w))){count++;}}returncount;}privatebooleanisSubsequenceBrute(Strings,Stringword){inti=0,j=0;while(i<s.length()&&j<word.length()){if(s.charAt(i)==word.charAt(j)){j++;}i++;}returnj==word.length();}}时间复杂度:
空间复杂度:
预处理:
单词检查:
“a”:
“bb”:
“acd”:
“ace”:
结果:3个单词匹配
publicstaticvoidmain(String[]args){Solutionsolution=newSolution();// 测试用例1:标准示例String[]words1={"a","bb","acd","ace"};System.out.println("Test 1: "+solution.numMatchingSubseq("abcde",words1));// 3// 测试用例2:重复单词String[]words2={"a","a","a"};System.out.println("Test 2: "+solution.numMatchingSubseq("abcde",words2));// 3// 测试用例3:空单词String[]words3={""};System.out.println("Test 3: "+solution.numMatchingSubseq("abcde",words3));// 1// 测试用例4:无匹配String[]words4={"bb","cb","bd"};System.out.println("Test 4: "+solution.numMatchingSubseq("abcde",words4));// 0// 测试用例5:完全匹配String[]words5={"abcde"};System.out.println("Test 5: "+solution.numMatchingSubseq("abcde",words5));// 1// 测试用例6:长字符串StringlongS="abcdefghijklmnopqrstuvwxyz";String[]words6={"ace","xyz","aeiou","bcdfg"};System.out.println("Test 6: "+solution.numMatchingSubseq(longS,words6));// 4// 测试用例7:单字符sString[]words7={"a","b","c"};System.out.println("Test 7: "+solution.numMatchingSubseq("a",words7));// 1// 测试用例8:大量重复单词String[]words8=newString[5000];Arrays.fill(words8,"ace");System.out.println("Test 8: "+solution.numMatchingSubseq("abcde",words8));// 5000// 测试用例9:边界情况String[]words9={"a","z"};System.out.println("Test 9: "+solution.numMatchingSubseq("a",words9));// 1// 测试用例10:空sString[]words10={"a",""};System.out.println("Test 10: "+solution.numMatchingSubseq("",words10));// 1 (只有空字符串匹配)}缓存:
words数组可能包含大量重复单词二分查找:
ss和短单词,效率提升子序列:
字符位置:
边界情况处理:
s中不存在的情况为什么需要缓存?
words可能包含重复单词二分查找?
prevIndex的位置