Skip to content

Commit 3bd7375

Browse files
authored
Added tasks 2185, 2186.
1 parent 17b9020 commit 3bd7375

File tree

7 files changed

+150
-0
lines changed

7 files changed

+150
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,8 @@ implementation 'com.github.javadev:leetcode-in-java:1.10'
13771377
| 2190 |[Most Frequent Number Following Key In an Array](src/main/java/g2101_2200/s2190_most_frequent_number_following_key_in_an_array/Solution.java)| Easy | Array, Hash_Table, Counting | 1 | 100.00
13781378
| 2188 |[Minimum Time to Finish the Race](src/main/java/g2101_2200/s2188_minimum_time_to_finish_the_race/Solution.java)| Hard | Array, Dynamic_Programming | 15 | 93.69
13791379
| 2187 |[Minimum Time to Complete Trips](src/main/java/g2101_2200/s2187_minimum_time_to_complete_trips/Solution.java)| Medium | Array, Binary_Search | 187 | 95.03
1380+
| 2186 |[Minimum Number of Steps to Make Two Strings Anagram II](src/main/java/g2101_2200/s2186_minimum_number_of_steps_to_make_two_strings_anagram_ii/Solution.java)| Medium | String, Hash_Table, Counting | 22 | 77.11
1381+
| 2185 |[Counting Words With a Given Prefix](src/main/java/g2101_2200/s2185_counting_words_with_a_given_prefix/Solution.java)| Easy | Array, String | 0 | 100.00
13801382
| 2183 |[Count Array Pairs Divisible by K](src/main/java/g2101_2200/s2183_count_array_pairs_divisible_by_k/Solution.java)| Hard | Array, Math, Number_Theory | 849 | 44.54
13811383
| 2182 |[Construct String With Repeat Limit](src/main/java/g2101_2200/s2182_construct_string_with_repeat_limit/Solution.java)| Medium | String, Greedy, Heap_Priority_Queue, Counting | 26 | 96.11
13821384
| 2181 |[Merge Nodes in Between Zeros](src/main/java/g2101_2200/s2181_merge_nodes_in_between_zeros/Solution.java)| Medium | Simulation, Linked_List | 6 | 96.26
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g2101_2200.s2185_counting_words_with_a_given_prefix;
2+
3+
// #Easy #Array #String #2022_06_08_Time_0_ms_(100.00%)_Space_43.9_MB_(15.40%)
4+
5+
public class Solution {
6+
public int prefixCount(String[] words, String pref) {
7+
int count = 0;
8+
for (String s : words) {
9+
if (s.startsWith(pref)) {
10+
count++;
11+
}
12+
}
13+
return count;
14+
}
15+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2185\. Counting Words With a Given Prefix
2+
3+
Easy
4+
5+
You are given an array of strings `words` and a string `pref`.
6+
7+
Return _the number of strings in_ `words` _that contain_ `pref` _as a **prefix**_.
8+
9+
A **prefix** of a string `s` is any leading contiguous substring of `s`.
10+
11+
**Example 1:**
12+
13+
**Input:** words = ["pay","**at**tention","practice","**at**tend"], `pref` \= "at"
14+
15+
**Output:** 2
16+
17+
**Explanation:** The 2 strings that contain "at" as a prefix are: "**at**tention" and "**at**tend".
18+
19+
**Example 2:**
20+
21+
**Input:** words = ["leetcode","win","loops","success"], `pref` \= "code"
22+
23+
**Output:** 0
24+
25+
**Explanation:** There are no strings that contain "code" as a prefix.
26+
27+
**Constraints:**
28+
29+
* `1 <= words.length <= 100`
30+
* `1 <= words[i].length, pref.length <= 100`
31+
* `words[i]` and `pref` consist of lowercase English letters.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2101_2200.s2186_minimum_number_of_steps_to_make_two_strings_anagram_ii;
2+
3+
// #Medium #String #Hash_Table #Counting #2022_06_08_Time_22_ms_(77.11%)_Space_70.1_MB_(39.92%)
4+
5+
public class Solution {
6+
public int minSteps(String s, String t) {
7+
int[] a = new int[26];
8+
for (int i = 0; i < s.length(); i++) {
9+
a[s.charAt(i) - 'a']++;
10+
}
11+
for (int i = 0; i < t.length(); i++) {
12+
a[t.charAt(i) - 'a']--;
13+
}
14+
int sum = 0;
15+
for (int j : a) {
16+
sum += Math.abs(j);
17+
}
18+
return sum;
19+
}
20+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2186\. Minimum Number of Steps to Make Two Strings Anagram II
2+
3+
Medium
4+
5+
You are given two strings `s` and `t`. In one step, you can append **any character** to either `s` or `t`.
6+
7+
Return _the minimum number of steps to make_ `s` _and_ `t` _**anagrams** of each other._
8+
9+
An **anagram** of a string is a string that contains the same characters with a different (or the same) ordering.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "**lee**tco**de**", t = "co**a**t**s**"
14+
15+
**Output:** 7
16+
17+
**Explanation:**
18+
19+
- In 2 steps, we can append the letters in "as" onto s = "leetcode", forming s = "leetcode**as**".
20+
21+
- In 5 steps, we can append the letters in "leede" onto t = "coats", forming t = "coats**leede**".
22+
23+
"leetcodeas" and "coatsleede" are now anagrams of each other.
24+
25+
We used a total of 2 + 5 = 7 steps.
26+
27+
It can be shown that there is no way to make them anagrams of each other with less than 7 steps.
28+
29+
**Example 2:**
30+
31+
**Input:** s = "night", t = "thing"
32+
33+
**Output:** 0
34+
35+
**Explanation:** The given strings are already anagrams of each other. Thus, we do not need any further steps.
36+
37+
**Constraints:**
38+
39+
* <code>1 <= s.length, t.length <= 2 * 10<sup>5</sup></code>
40+
* `s` and `t` consist of lowercase English letters.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g2101_2200.s2185_counting_words_with_a_given_prefix;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void prefixCount() {
11+
assertThat(
12+
new Solution()
13+
.prefixCount(new String[] {"pay", "attention", "practice", "attend"}, "at"),
14+
equalTo(2));
15+
}
16+
17+
@Test
18+
void prefixCount2() {
19+
assertThat(
20+
new Solution()
21+
.prefixCount(new String[] {"leetcode", "win", "loops", "success"}, "code"),
22+
equalTo(0));
23+
}
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2101_2200.s2186_minimum_number_of_steps_to_make_two_strings_anagram_ii;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minSteps() {
11+
assertThat(new Solution().minSteps("leetcode", "coats"), equalTo(7));
12+
}
13+
14+
@Test
15+
void minSteps2() {
16+
assertThat(new Solution().minSteps("night", "thing"), equalTo(0));
17+
}
18+
}

0 commit comments

Comments
 (0)