forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_1170.java
139 lines (128 loc) · 4.99 KB
/
_1170.java
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.fishercoder.solutions;
import java.util.Arrays;
/**
* 1170. Compare Strings by Frequency of the Smallest Character
*
* Let's define a function f(s) over a non-empty string s,
* which calculates the frequency of the smallest character in s.
* For example, if s = "dcce" then f(s) = 2 because the smallest character is "c" and its frequency is 2.
* Now, given string arrays queries and words,
* return an integer array answer,
* where each answer[i] is the number of words such that f(queries[i]) < f(W), where W is a word in words.
*
* Example 1:
* Input: queries = ["cbd"], words = ["zaaaz"]
* Output: [1]
* Explanation: On the first query we have f("cbd") = 1, f("zaaaz") = 3 so f("cbd") < f("zaaaz").
*
* Example 2:
* Input: queries = ["bbb","cc"], words = ["a","aa","aaa","aaaa"]
* Output: [1,2]
* Explanation: On the first query only f("bbb") < f("aaaa"). On the second query both f("aaa") and f("aaaa") are both > f("cc").
*
* Constraints:
* 1 <= queries.length <= 2000
* 1 <= words.length <= 2000
* 1 <= queries[i].length, words[i].length <= 10
* queries[i][j], words[i][j] are English lowercase letters.
* */
public class _1170 {
public static class Solution1 {
/**
* Use simple iteration when finding counts
* Time: O(n^m) where m is the size of queries and n is the size of words
* Space: O(max(m, n) where m is the size of queries and n is the size of words)
* */
public int[] numSmallerByFrequency(String[] queries, String[] words) {
int[] queriesMinFrequecies = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
queriesMinFrequecies[i] = computeLowestFrequency(queries[i]);
}
int[] wordsMinFrequecies = new int[words.length];
for (int i = 0; i < words.length; i++) {
wordsMinFrequecies[i] = computeLowestFrequency(words[i]);
}
Arrays.sort(wordsMinFrequecies);
int[] result = new int[queries.length];
for (int i = 0; i < result.length; i++) {
result[i] = search(wordsMinFrequecies, queriesMinFrequecies[i]);
}
return result;
}
private int search(int[] nums, int target) {
int count = 0;
for (int i = nums.length - 1; i >= 0; i--) {
if (nums[i] > target) {
count++;
} else {
break;
}
}
return count;
}
private int computeLowestFrequency(String string) {
char[] str = string.toCharArray();
Arrays.sort(str);
String sortedString = new String(str);
int frequency = 1;
for (int i = 1; i < sortedString.length(); i++) {
if (sortedString.charAt(i) == sortedString.charAt(0)) {
frequency++;
} else {
break;
}
}
return frequency;
}
}
public static class Solution2 {
/**
* Use binary search when finding counts
* Time: O(n^logn) where m is the size of queries and n is the size of words
* Space: O(max(m, n) where m is the size of queries and n is the size of words)
* */
public int[] numSmallerByFrequency(String[] queries, String[] words) {
int[] queriesMinFrequecies = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
queriesMinFrequecies[i] = computeLowestFrequency(queries[i]);
}
int[] wordsMinFrequecies = new int[words.length];
for (int i = 0; i < words.length; i++) {
wordsMinFrequecies[i] = computeLowestFrequency(words[i]);
}
Arrays.sort(wordsMinFrequecies);
int[] result = new int[queries.length];
for (int i = 0; i < result.length; i++) {
result[i] = binarySearch(wordsMinFrequecies, queriesMinFrequecies[i]);
}
return result;
}
private int binarySearch(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (nums[mid] <= target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return nums.length - left;
}
private int computeLowestFrequency(String string) {
char[] str = string.toCharArray();
Arrays.sort(str);
String sortedString = new String(str);
int frequency = 1;
for (int i = 1; i < sortedString.length(); i++) {
if (sortedString.charAt(i) == sortedString.charAt(0)) {
frequency++;
} else {
break;
}
}
return frequency;
}
}
}