Skip to content

Commit ac140b8

Browse files
add one solution for 1170
1 parent cacc4e7 commit ac140b8

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Your ideas/fixes/algorithms are more than welcome!
3838
|1154|[Day of the Year](https://leetcode.com/problems/day-of-the-year/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1154.java) | O(1) | O(1) | |Easy||
3939
|1137|[N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1137.java) | O(n) | O(n) | |Easy||
4040
|1122|[Relative Sort Array](https://leetcode.com/problems/relative-sort-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1122.java) | O(n) | O(n) | |Easy||
41+
|1170|[Compare Strings by Frequency of the Smallest Character](https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1170.java) | | | |Easy||
4142
|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1089.java) | O(n^2) | O(1) | |Easy||
4243
|1079|[Letter Tile Possibilities](https://leetcode.com/problems/letter-tile-possibilities/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1079.java) | O(1) | O(1) | |Medium||
4344
|1078|[Occurrences After Bigram](https://leetcode.com/problems/occurrences-after-bigram/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1078.java) | O(n) | O(1) | |Easy||

src/main/java/com/fishercoder/solutions/_1170.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.Arrays;
4+
35
/**
46
* 1170. Compare Strings by Frequency of the Smallest Character
57
*
@@ -28,8 +30,55 @@
2830
* */
2931
public class _1170 {
3032
public static class Solution1 {
33+
/**
34+
* Use simple iteration when finding counts
35+
* Time: O(n^m) where m is the size of queries and n is the size of words
36+
* Space: O(max(m, n) where m is the size of queries and n is the size of words)
37+
* */
3138
public int[] numSmallerByFrequency(String[] queries, String[] words) {
32-
return null;
39+
int[] queriesMinFrequecies = new int[queries.length];
40+
for (int i = 0; i < queries.length; i++) {
41+
queriesMinFrequecies[i] = computeLowestFrequency(queries[i]);
42+
}
43+
44+
int[] wordsMinFrequecies = new int[words.length];
45+
for (int i = 0; i < words.length; i++) {
46+
wordsMinFrequecies[i] = computeLowestFrequency(words[i]);
47+
}
48+
Arrays.sort(wordsMinFrequecies);
49+
50+
int[] result = new int[queries.length];
51+
for (int i = 0; i < result.length; i++) {
52+
result[i] = search(wordsMinFrequecies, queriesMinFrequecies[i]);
53+
}
54+
return result;
55+
}
56+
57+
private int search(int[] nums, int target) {
58+
int count = 0;
59+
for (int i = nums.length - 1; i >= 0; i--) {
60+
if (nums[i] > target) {
61+
count++;
62+
} else {
63+
break;
64+
}
65+
}
66+
return count;
67+
}
68+
69+
private int computeLowestFrequency(String string) {
70+
char[] str = string.toCharArray();
71+
Arrays.sort(str);
72+
String sortedString = new String(str);
73+
int frequency = 1;
74+
for (int i = 1; i < sortedString.length(); i++) {
75+
if (sortedString.charAt(i) == sortedString.charAt(0)) {
76+
frequency++;
77+
} else {
78+
break;
79+
}
80+
}
81+
return frequency;
3382
}
3483
}
3584
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1170;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _1170Test {
10+
private static _1170.Solution1 solution1;
11+
private static String[] queries;
12+
private static String[] words;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _1170.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
queries = new String[]{"cbd"};
22+
words = new String[]{"zaaaz"};
23+
assertArrayEquals(new int[]{1}, solution1.numSmallerByFrequency(queries, words));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
queries = new String[]{"bbb", "cc"};
29+
words = new String[]{"a", "aa", "aaa", "aaaa"};
30+
assertArrayEquals(new int[]{1, 2}, solution1.numSmallerByFrequency(queries, words));
31+
}
32+
33+
}

0 commit comments

Comments
 (0)