lifeiteng

686. Repeated String Match

Sep 11th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.53 KB | None | 0 0
  1. // 实话实说没觉得特别easy。。
  2. class Solution {
  3.     public int repeatedStringMatch(String A, String B) {
  4.         if(A == null || B == null) return -1;
  5.         if(B.equals("")) return 1;
  6.         int m = A.length(), n = B.length(), count = 0;
  7.         StringBuilder sb = new StringBuilder();
  8.         while(sb.length() < n)
  9.         {
  10.             sb.append(A);
  11.             count++;
  12.         }
  13.         if(sb.indexOf(B) >= 0) return count;
  14.         if(sb.append(A).indexOf(B) >= 0) return count + 1;
  15.         return -1;
  16.     }
  17. }
Add Comment
Please, Sign In to add comment