knakul853

Untitled

Jun 26th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. /**
  2. knakul853
  3. **/
  4. class Solution {
  5. public:
  6.     bool repeatedSubstringPattern(string s) {
  7.        
  8.         int n = (int)s.size();
  9.         vector<int>dp(n, 0);
  10.        
  11.          for(int i=1;i<n;i++)
  12.          {
  13.              int j = dp[i-1];
  14.              
  15.              while(j>=1 && s[i] != s[j] )j = dp[j-1];
  16.            
  17.              if(s[i] == s[j])
  18.              {
  19.                  j++;
  20.              }
  21.              
  22.              dp[i] = j;
  23.          }
  24.        
  25.         return dp[n-1] && dp[n-1] % (n - dp[n-1]) == 0;
  26.        
  27.     }
  28. };
Add Comment
Please, Sign In to add comment