You are viewing a single comment's thread. Return to all comments →
using z - algorithem all test case pass
static long stringSimilarity(String str) { long c=str.length(); int L = 0, R = 0,n=str.length(); char[] s = str.toCharArray(); int []z=new int[n]; for (int i = 1; i < n; i++) { if (i > R) { L = R = i; while (R < n && s[R-L] == s[R]) R++; z[i] = R-L; R--; c+=z[i]; } else { int k = i-L; if (z[k] < R-i+1) { z[i] = z[k]; c+=z[i]; } else { L = i; while (R < n && s[R-L] == s[R]) R++; z[i] = R-L; c+=z[i]; R--; } } } return c; }
Seems like cookies are disabled on this browser, please enable them to open this website
String Similarity
You are viewing a single comment's thread. Return to all comments →
using z - algorithem all test case pass