书籍判断两个字符串是否互为旋转词

题目

如果一个字符串str,把字符串str前面任意的部分挪到后面形成的字符串叫作str的旋转词。比如str="12345",str的旋转词有"12345","23451","34512","45123"和"51234"。给定两个字符串a和b,请判断a和b是否互为旋转词。

举例

a="cdab",b="abcd" 返回true

a="1ab2",b="ab12" 返回false

a="2ab1",b="ab12" 返回true

如果a和b长度不一样,那么a和b必然不互为旋转词,可以直接返回false。当a和b长度一样,都为N时,要求解法的时间复杂度为O(N).

如果a和b长度一样,先生成一个大字符串b2,b2是两个字符串b拼在一起的结果,即String b2 = b +b。

java 复制代码
public class KMP {

    public static boolean isRotation(String a, String b) {
        if(a == null || b == null || a.length() != b.length()){
            return false;
        }
        String b2 = b + b;
        return kmpSearch(b2, a) != -1;
    }

    public static int[] compteNext(String pattern) {
        int[] next = new int[pattern.length()];
        int j = 0;
        next[0] = j;
        for (int i = 1; i < pattern.length(); i++) {
            while (j > 0 && pattern.charAt(i)!= pattern.charAt(j)) {
                j = next[j - 1];
            }
            if (pattern.charAt(i) == pattern.charAt(j)) {
                j++;
            }
            next[i] = j;
        }
        return next;
    }

    public static int kmpSearch(String text, String pattern) {
        int[] next = compteNext(pattern);
        int i = 0, j = 0;
        while (i < text.length() && j < pattern.length()) {
            if (text.charAt(i) == pattern.charAt(j)) {
                i++;
                j++;
            } else if (j > 0) {
                j = next[j - 1];
            } else {
                i++;
            }
        }
        if (j == pattern.length()) {
            return i - j;
        } else {
            return -1;
        }
    }

    public static void main(String[] args) {
        String text = "cdab";
        String pattern = "abcd";
        System.out.println("Pattern found at : " + isRotation(text, pattern));
    }
}
相关推荐
遇见你...16 小时前
A01-Spring概述
java·后端·spring
Via_Neo19 小时前
JAVA中以2为底的对数表示方式
java·开发语言
野生技术架构师20 小时前
一线大厂Java面试八股文全栈通关手册(含源码级详解)
java·开发语言·面试
袋鼠云数栈20 小时前
集团数字化统战实战:统一数据门户与全业态监管体系构建
大数据·数据结构·人工智能·多模态
廋到被风吹走20 小时前
【AI】Codex 多语言实测:Python/Java/JS/SQL 效果横评
java·人工智能·python
tERS ERTS20 小时前
MySQL中查看表结构
java
坊钰20 小时前
Java 死锁问题及其解决方案
java·开发语言·数据库
于先生吖20 小时前
SpringBoot+MQTT 无人健身房智能管控系统源码实战
java·spring boot·后端
小月球~20 小时前
天梯赛 · 并查集
数据结构·算法
仍然.21 小时前
算法题目---模拟
java·javascript·算法