Advertisement
1988coder

686. Repeated String Match

Jan 5th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. // LeetCode URL: https://leetcode.com/problems/repeated-string-match/
  2. /**
  3.  * Time Complexity: O((A + B) * B)
  4.  *
  5.  * A+B = Length of the new string in stringbuilder.
  6.  *
  7.  * Space Complexity: O(A + B)
  8.  */
  9. class Solution1 {
  10.     public int repeatedStringMatch(String A, String B) {
  11.         if (A == null || B == null) {
  12.             return -1;
  13.         }
  14.         if (B.length() == 0 || B.equals(A)) {
  15.             return 1;
  16.         }
  17.         if (A.length() == 0) {
  18.             return -1;
  19.         }
  20.  
  21.         int count = 0;
  22.         StringBuilder sb = new StringBuilder();
  23.         while (sb.length() < B.length()) {
  24.             sb.append(A);
  25.             count++;
  26.         }
  27.  
  28.         if (sb.indexOf(B) >= 0) {
  29.             return count;
  30.         }
  31.  
  32.         sb.append(A);
  33.         count++;
  34.  
  35.         if (sb.indexOf(B) >= 0) {
  36.             return count;
  37.         }
  38.  
  39.         return -1;
  40.     }
  41. }
  42.  
  43. /**
  44.  * Using KMP for string contains.
  45.  *
  46.  * Time Complexity: O((A + B) + B)
  47.  *
  48.  * A+B = Length of the new string in stringbuilder.
  49.  *
  50.  * Space Complexity: O(A + B)
  51.  */
  52. class Solution2 {
  53.     public int repeatedStringMatch(String A, String B) {
  54.         if (A == null || B == null) {
  55.             return -1;
  56.         }
  57.         if (B.length() == 0 || A.equals(B)) {
  58.             return 1;
  59.         }
  60.         if (A.length() == 0) {
  61.             return -1;
  62.         }
  63.  
  64.         int count = 0;
  65.         StringBuilder sb = new StringBuilder();
  66.         while (sb.length() < B.length()) {
  67.             sb.append(A);
  68.             count++;
  69.         }
  70.  
  71.         int[] lookupTable = kmpLookupTable(B);
  72.  
  73.         if (contains(sb.toString(), B, lookupTable)) {
  74.             return count;
  75.         }
  76.  
  77.         sb.append(A);
  78.         count++;
  79.  
  80.         if (contains(sb.toString(), B, lookupTable)) {
  81.             return count;
  82.         }
  83.  
  84.         return -1;
  85.     }
  86.  
  87.     private int[] kmpLookupTable(String s) {
  88.         int[] table = new int[s.length()];
  89.         int i = 1;
  90.         int index = 0;
  91.         while (i < s.length()) {
  92.             if (s.charAt(i) == s.charAt(index)) {
  93.                 index++;
  94.                 table[i] = index;
  95.                 i++;
  96.             } else {
  97.                 if (index > 0) {
  98.                     index = table[index - 1];
  99.                 } else {
  100.                     i++;
  101.                 }
  102.             }
  103.         }
  104.         return table;
  105.     }
  106.  
  107.     private boolean contains(String haystack, String needle, int[] table) {
  108.         int i = 0;
  109.         int j = 0;
  110.         while (i < haystack.length() && j < needle.length()) {
  111.             if (haystack.charAt(i) == needle.charAt(j)) {
  112.                 i++;
  113.                 j++;
  114.             } else {
  115.                 if (j > 0) {
  116.                     j = table[j - 1];
  117.                 } else {
  118.                     i++;
  119.                 }
  120.             }
  121.         }
  122.         if (j == needle.length()) {
  123.             return true;
  124.         } else {
  125.             return false;
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement