zoukankan      html  css  js  c++  java
  • LeetCode Repeated Substring Pattern

    原题链接在这里:https://leetcode.com/problems/repeated-substring-pattern/

    题目:

    Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.

    Example 1:

    Input: "abab"
    
    Output: True
    
    Explanation: It's the substring "ab" twice.

    Example 2:

    Input: "aba"
    
    Output: False

    Example 3:

    Input: "abcabcabcabc"
    
    Output: True
    
    Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)

    题解:

    若是str能被拆能n个重复的子串, 那么每个子串的长度不会超过str.length()/2. 从str.length()/2到1长度,挨个试取字串拼回去,看是否等于原串.

    Time Complexity: O(n^2). 拆成n/2, 拼回去用时2, 拆成n/3, 拼回去用时3, ..., 总用时(2+3+4+...+n), n = str.length().

    Space: O(1). 

    AC Java:

     1 public class Solution {
     2     public boolean repeatedSubstringPattern(String str) {
     3         int len = str.length();
     4         for(int i = len/2; i>=1; i--){
     5             if(len%i == 0){
     6                 String template = str.substring(0, i);
     7                 StringBuilder sb = new StringBuilder();
     8                 int n = len/i;
     9                 while(n-->0){
    10                     sb.append(template);
    11                 }
    12                 if(sb.toString().equals(str)){
    13                     return true;
    14                 }
    15             }
    16         }
    17         return false;
    18     }
    19 }

    可采用类似KMP的算法. 这里的next不再是当前char之前的最大相同前缀后缀,而是包含当前char的最大相同前缀后缀. e.g "abcabcabc", next = [0,0,0,1,2,3,4,5,6].

    看str.length()能否被str.length() - next[next.length-1]整除.

    Time Complexity: O(str.length()). Space: O(str.length()).

    AC Java:

     1 public class Solution {
     2     public boolean repeatedSubstringPattern(String str) {
     3         if(str == null || str.length() == 0){
     4             return true;
     5         }
     6         int len = str.length();
     7         int [] next = new int[len];
     8         getNext(str, next);
     9         int preLen = next[next.length-1];
    10         return (preLen>0 && len%(len-preLen) == 0);
    11     }
    12     
    13     private void getNext(String s, int [] next){
    14         int len = s.length();
    15         int p = 0;
    16         int q = 1;
    17         while(q < len){
    18             //s.charAt(p)表示前缀. s.charAt(q)表示后缀
    19             if(s.charAt(p) == s.charAt(q)){
    20                 next[q] = p+1;
    21                 p++;
    22                 q++;
    23             }else if(p == 0){
    24                 next[q] = p;
    25                 q++;
    26             }else{
    27                 p = next[p-1];
    28             }
    29         }
    30     }
    31 }

    类似Implement strStr().

  • 相关阅读:
    移动端触摸右侧菜单栏,页面内容对应项滚动到最上方
    swiper使用中一些点的总结
    javaScript正则表达式入门
    javaScript之数组操作方法(一)
    初识vue
    焦点控制切换和轮播
    文本内容只显示两行,然后加...
    图片父容器高度不定的图片垂直居中
    css3图片垂直居中
    【C#】两个list根据某个元素比较差集
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/6321786.html
Copyright © 2011-2022 走看看