Unique Substrings With Equal Digit Frequency
给一个string, 求unique的相同字频的子字符串.
这题吧, 我看用rolling hash做能快点, 但是我用的count做的.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
class Solution { public int equalDigitFrequency(String s) { Set<String> set = new HashSet<>(); int n = s.length(); for(int i = 0; i < n; i++) { int[] count = new int[26]; for(int j = i; j < n; j++) { count[s.charAt(j) - '0']++; if(check(count)) set.add(s.substring(i, j + 1)); } } return set.size(); } public boolean check(int[] count) { int nn = -1; for(int n : count){ if(n != 0) { if(nn == -1) nn = n; else{ if(nn != n) return false; } } } return true; } } |