Compare Strings by Frequency of the Smallest Character
给两个字符串组, 比较两个字符串组的最小char的频率. 我就直接做的, 我看答案有二叉搜索
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 31 32 |
class Solution { public int[] numSmallerByFrequency(String[] queries, String[] words) { int[] t = new int[words.length]; for(int i = 0; i < words.length; i++){ t[i] = count(words[i]); } int[] res = new int[queries.length]; for(int i = 0; i < queries.length; i++) { int c = 0; int tt = count(queries[i]); for(int j = 0; j < t.length; j++) { if(tt < t[j]) c++; } res[i] = c; } return res; } private int count (String s) { char[] c = s.toCharArray(); Arrays.sort(c); char t = c[0]; int count = 0; for(int i = 0; i < c.length; i++){ if(t != c[i]) break; count++; } return count; } } |