Skip to content

Commit 8affa18

Browse files
authored
Added task 2269.
1 parent 19e2420 commit 8affa18

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.10'
13691369
| 2286 |[Booking Concert Tickets in Groups](src/main/java/g2201_2300/s2286_booking_concert_tickets_in_groups/BookMyShow.java)| Hard | Binary_Search, Design, Binary_Indexed_Tree, Segment_Tree | 283 | 67.08
13701370
| 2280 |[Minimum Lines to Represent a Line Chart](src/main/java/g2201_2300/s2280_minimum_lines_to_represent_a_line_chart/Solution.java)| Medium | Array, Math, Geometry, Sorting, Number_Theory | 40 | 96.09
13711371
| 2274 |[Maximum Consecutive Floors Without Special Floors](src/main/java/g2201_2300/s2274_maximum_consecutive_floors_without_special_floors/Solution.java)| Medium | Array, Sorting | 33 | 99.36
1372+
| 2269 |[Find the K-Beauty of a Number](src/main/java/g2201_2300/s2269_find_the_k_beauty_of_a_number/Solution.java)| Easy | Math, String, Sliding_Window | 2 | 38.88
13721373
| 2260 |[Minimum Consecutive Cards to Pick Up](src/main/java/g2201_2300/s2260_minimum_consecutive_cards_to_pick_up/Solution.java)| Medium | Array, Hash_Table, Sliding_Window | 50 | 97.04
13731374
| 2259 |[Remove Digit From Number to Maximize Result](src/main/java/g2201_2300/s2259_remove_digit_from_number_to_maximize_result/Solution.java)| Easy | String, Greedy, Enumeration | 1 | 97.73
13741375
| 2257 |[Count Unguarded Cells in the Grid](src/main/java/g2201_2300/s2257_count_unguarded_cells_in_the_grid/Solution.java)| Medium | Array, Matrix, Simulation | 32 | 70.28
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g2201_2300.s2269_find_the_k_beauty_of_a_number;
2+
3+
// #Easy #Math #String #Sliding_Window #2022_06_15_Time_2_ms_(38.88%)_Space_41.3_MB_(41.96%)
4+
5+
public class Solution {
6+
public int divisorSubstrings(int num, int k) {
7+
int i = 0;
8+
int j = 0;
9+
int count = 0;
10+
String s = String.valueOf(num);
11+
StringBuilder sb = new StringBuilder();
12+
while (i < s.length() && j < s.length()) {
13+
sb.append(s.charAt(j) - '0');
14+
int val = Integer.parseInt(sb.toString());
15+
if (j - i + 1 == k) {
16+
if (val != 0 && num % val == 0) {
17+
count++;
18+
}
19+
sb.deleteCharAt(0);
20+
i++;
21+
j++;
22+
} else {
23+
j++;
24+
}
25+
}
26+
return count;
27+
}
28+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2269\. Find the K-Beauty of a Number
2+
3+
Easy
4+
5+
The **k-beauty** of an integer `num` is defined as the number of **substrings** of `num` when it is read as a string that meet the following conditions:
6+
7+
* It has a length of `k`.
8+
* It is a divisor of `num`.
9+
10+
Given integers `num` and `k`, return _the k-beauty of_ `num`.
11+
12+
Note:
13+
14+
* **Leading zeros** are allowed.
15+
* `0` is not a divisor of any value.
16+
17+
A **substring** is a contiguous sequence of characters in a string.
18+
19+
**Example 1:**
20+
21+
**Input:** num = 240, k = 2
22+
23+
**Output:** 2
24+
25+
**Explanation:** The following are the substrings of num of length k:
26+
27+
- "24" from "**24**0": 24 is a divisor of 240.
28+
29+
- "40" from "2**40**": 40 is a divisor of 240.
30+
31+
Therefore, the k-beauty is 2.
32+
33+
**Example 2:**
34+
35+
**Input:** num = 430043, k = 2
36+
37+
**Output:** 2
38+
39+
**Explanation:** The following are the substrings of num of length k:
40+
41+
- "43" from "**43**0043": 43 is a divisor of 430043.
42+
43+
- "30" from "4**30**043": 30 is not a divisor of 430043.
44+
45+
- "00" from "43**00**43": 0 is not a divisor of 430043.
46+
47+
- "04" from "430**04**3": 4 is not a divisor of 430043.
48+
49+
- "43" from "4300**43**": 43 is a divisor of 430043.
50+
51+
Therefore, the k-beauty is 2.
52+
53+
**Constraints:**
54+
55+
* <code>1 <= num <= 10<sup>9</sup></code>
56+
* `1 <= k <= num.length` (taking `num` as a string)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2201_2300.s2269_find_the_k_beauty_of_a_number;
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 divisorSubstrings() {
11+
assertThat(new Solution().divisorSubstrings(240, 2), equalTo(2));
12+
}
13+
14+
@Test
15+
void divisorSubstrings2() {
16+
assertThat(new Solution().divisorSubstrings(430043, 2), equalTo(2));
17+
}
18+
}

0 commit comments

Comments
 (0)