第四章 字符串part02

94 阅读2分钟

28. Find the Index of the First Occurrence in a String

Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

题目解析:

  • 双重循环找到目标index,时间复杂度O(nm)
  • KMP,时间复杂度O(n+m)

代码:
1. 双重循环

class Solution {
    public int strStr(String haystack, String needle) {
        int sizeH = haystack.length(), sizeN = needle.length();
        for (int i = 0; i <= sizeH - sizeN; i++) {
            if (haystack.charAt(i) == needle.charAt(0)) {
                for (int j = 0; j < needle.length(); j++) {
                    if (needle.charAt(j) != haystack.charAt(i+j)) {
                        break;
                    }
                    if (j == needle.length() - 1) {
                        return i;
                    }
                }
            }
        }
        return -1;
    }
}

2. KMP

lass Solution {
    public int strStr(String haystack, String needle) {
        int[] next = getNext(needle);
        System.out.println(Arrays.toString(next));
        int j = 0;
        for (int i = 0; i < haystack.length(); i++) {
            while (j > 0 && needle.charAt(j) != haystack.charAt(i)) {
                j = next[j-1];
            }
            if (needle.charAt(j) == haystack.charAt(i)) {
                j++;
            }
            if (j == needle.length()) {
                return i - j + 1;
            }
        }
        return -1;
    }

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

459. Repeated Substring Pattern

Given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.

题目解析:

  • 暴力使用双重循环,时间复杂度为O(n^2)
  • 使用移动匹配,如果字符串存在循环子串,那么字符串的后部分(比如下标 1n-1)加上字符串的前部分(比如下标0n-2)所组成的新字符串一定包含原字符串,再使用kmp算法判断是否包含,时间复杂度为O(n)
  • 使用KMP找到最长相等前后缀,字符串中不包含相等前后缀的部分为最小重复子串,时间复杂度为O(n) 参考

代码
1. 移动匹配

class Solution {
    public boolean repeatedSubstringPattern(String s) {
        char[] arr = new String(s+s).toCharArray();
        return find(new String(arr, 1, arr.length - 2), s);
    }

    public boolean find(String origin, String target) {
        int j = 0;
        int[] next = getNext(target);
        for (int i = 0; i < origin.length(); i++) {
            while (j > 0 && origin.charAt(i) != target.charAt(j)) {
                j = next[j-1];
            }
            if (origin.charAt(i) == target.charAt(j)) {
                j++;
            }
            if (j == target.length()) {
                return true;
            }
        }
        return false;
    }
    
    public int[] getNext(String s) {
        int[] next = new int[s.length()]; 
        int j = 0;
        for (int i = 1; i < s.length(); i++) {
            while(j > 0 && s.charAt(i) != s.charAt(j)) {
                j = next[j - 1];
            }
            if (s.charAt(i) == s.charAt(j)) {
                next[i] = ++j;
            }
        }
        return next;
    }
}

2. KMP

class Solution {
    public boolean repeatedSubstringPattern(String s) {
        int j = 0;
        int[] next = new int[s.length()];
        for (int i = 1; i < s.length(); i++) {
            while (j > 0 && s.charAt(i) != s.charAt(j)) {
                j = next[j - 1];
            }
            if (s.charAt(i) == s.charAt(j)) {
                next[i] = ++j;
            }
        }
        System.out.println(Arrays.toString(next));
        if (next[s.length()-1] > 0 && s.length() % (s.length() - next[s.length()-1]) == 0) {
            return true;
        }
        return false;
    }
}

总结

  1. 字符串上操作经常使用双指针
  2. 判断字符串中是否包含另一个字符串,比较高效的方法是KMP (KMP也用了双指针)
  3. 按空格切分字符串可以使用正则表达式\\s+
  4. 获取子串可以使用substring
  5. 字符数组转字符串使用new String(arr, 0, n)