Unique Substrings in Wraparound String
Thoughts
Code
class Solution {
public int findSubstringInWraproundString(String p) {
int n = p.length();
int[] f = new int[26];
int longestCont = 0;
for (int i = 0; i < n; i++) {
if (i > 0 && (p.charAt(i) == p.charAt(i - 1) + 1 || p.charAt(i - 1) == 'z' && p.charAt(i) == 'a')) {
longestCont++;
} else {
longestCont = 1;
}
int index = p.charAt(i) - 'a';
f[index] = Math.max(f[index], longestCont);
}
int sum = 0;
for (int i = 0; i < 26; i++) {
sum += f[i];
}
return sum;
}
}Analysis
Last updated