KMP算法代码实现记录
private static int kmpDemo(String str1,String str2){ if (str2==null||str1==null||str1.length()-str2.length()<0){ return -1; } //首先构建字符匹配表 int[] matchTable=getStrMatchTable(str2); for (int i = 0,j=0; i < str1.length(); i++) { while (j>0&&str1.charAt(i)!=str2.charAt(j)){ j=matchTable[j-1]; } if(str1.charAt(i)==str2.charAt(j)){ j++; } if(j==str2.length()){ return i-j+1; } } return -1; } //构建字符匹配表 private static int[] getStrMatchTable(String str){ int[] res=new int[str.length()]; res[0]=0; for (int i = 1,j=0; i < res.length; i++) { //当str.charAt(i) != str.charAt(j),需要从res[j-1]获取新的j的位置 //知道找到二者相等时才退出寻找 //这是kmp算法的核心 while (j > 0 && str.charAt(i) != str.charAt(j)) { j=res[j-1]; } //当str.charAt(i)==str.charAt(j)满足时,部分匹配值就+ if(str.charAt(i)==str.charAt(j)){ j++; } res[i]=j; } return res; }
======
class Solution { int maxK=Integer.MAX_VALUE; public int kSimilarity(String A, String B){ char[] arrA=new char[A.length()]; char[] arrB=new char[B.length()]; for (int i = 0; i < arrA.length; i++) { arrA[i]=A.charAt(i); arrB[i]=B.charAt(i); } doSelect(arrA,arrB,A.length(),0,0); return maxK; } /** * * @param arrA 字符串A char数组 * @param arrB 字符串B char数组 * @param len char数组的长度(为了方便使用) * @param curCount 当前交换的次数 * @param curIndex 当前交换位置的索引 */ private void doSelect(char[] arrA,char[] arrB,int len,int curCount,int curIndex){ if(curCount>=maxK){//已经不小于最小交换次数,我也没必要再找下去了 return; } if(curIndex==len-1){ if(curCount<maxK){ maxK=curCount; } } while (curIndex<len-1){ //若当前位置二者值不相同,那么我就需要去后面找当前A需要的char,进行交换 if(arrA[curIndex]!=arrB[curIndex]){ int pointer=curIndex+1; while (pointer<len){ if(arrB[pointer]==arrA[curIndex]){ swapChar(arrB,curIndex,pointer); doSelect(arrA,arrB,len,curCount+1,curIndex+1); swapChar(arrB,curIndex,pointer);//执行完本次交换回来,这样不影响其他 } pointer++; } break; }else { curIndex++; } } if (curIndex == len-1){ if (curCount < maxK) maxK = curCount; } } private void swapChar(char[] arrB,int i,int j){ char temp=arrB[i]; arrB[i]=arrB[j]; arrB[j]=temp; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗