Advertisement
codetalkinhawkin

Untitled

Mar 24th, 2020
595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1. /*
  2. Find whether a given string s is divisible by string t. If it is
  3. divisible, find the length of the smallest string x such that if x is concatenated any
  4.  number of times, we get both s and t. If this is not
  5.  possible, then print -1. Help find the length of the smallest string x.
  6.  A string s is said to be divisible by string t if string t can be
  7.  concatenated some number of times to get string s.
  8.  
  9. Constraints
  10.  
  11. 1 ≤ size of s ≤ 2 x 10^5
  12. 1 ≤ size of t ≤ 2 x 10^5
  13. size of t ≤ size of s
  14.  
  15.  Example:
  16.  s = bcdbcdbcd
  17.  t = bcdbcd
  18.  If string t is concatenated twice, the result
  19.  is bcdbcdbcdbcd > s. String s is not divisible by string t, so
  20.  the result is -1.
  21.  
  22.  Example:
  23.  s = bcdbcdbcdbcd
  24.  t = bcdbcd
  25.  
  26.  If string t is concatenated twice, the result
  27.  is bcdbcdbcdbcd = s. String s is divisible by string t. The
  28.  smallest string x that can be concatenated to create both
  29.  strings s and t is bcd. Its length is 3.
  30.  
  31. Sample Input
  32. lrbb
  33. lrbb
  34. Sample Output
  35. 4
  36. Explanation
  37. If string "lrbb" is concatenated 1 time, we get string
  38. s and string t.
  39.  
  40. Sample Input
  41. rbrb
  42. rbrb
  43. Sample Output
  44. 2
  45. Explanation
  46. If string "rb" is concatenated 2 times, we get string
  47. s and string t.
  48.  
  49. */
  50.  
  51.     private static int makeLPSArray(String str, int index,
  52.                                      int lps[], int longestSubLength) {
  53.         int len = 0;
  54.         int i = 1;
  55.         lps[0] = 0;
  56.  
  57.         while (i < index) {
  58.             if (str.charAt(i) == str.charAt(len)) {
  59.                 len++;
  60.                 lps[i] = len;
  61.                 i++;
  62.             }
  63.             else {
  64.                 if (len > 0) {
  65.                     len = lps[len-1];
  66.                 }
  67.             }
  68.         }
  69.         return longestSubLength;
  70.     }
  71.  
  72.     private static int calcRepetitions(String text, String sub) {
  73.         //for every text.substring(sub)
  74.         int reps = 0;
  75.         if(text.length() % sub.length() == 0){
  76.             for(int i = 0; i < text.length() &&
  77.                     i + sub.length() - 1 < text.length(); i++) {
  78.                 if(text.substring(i, i + sub.length()).equals(sub)){
  79.                     reps++;
  80.                     i += sub.length() - 1;
  81.                 }
  82.             }
  83.         }
  84.  
  85.         return reps;
  86.     }
  87.  
  88.     public static int findSmallestDivisor(String s, String t) {
  89.         int tReps = 0;
  90.         int sReps = 0;
  91.         int minDivisorSize = -1;
  92.         if(s.length() % t.length() == 0) {
  93.  
  94.             for (minDivisorSize = 1; minDivisorSize < t.length(); minDivisorSize++) {
  95.                 String repeat = t.substring(0, minDivisorSize);
  96.                 tReps = calcRepetitions(t, repeat);
  97.                 sReps = calcRepetitions(s, repeat);
  98.  
  99.                 if (sReps != 0 && tReps != 0 && tReps <= sReps)
  100.                     return minDivisorSize;
  101.             }
  102.  
  103.         }
  104.  
  105.         return minDivisorSize;
  106.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement