-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindTheK-BeautyOfANumber.java
More file actions
29 lines (28 loc) · 1.02 KB
/
FindTheK-BeautyOfANumber.java
File metadata and controls
29 lines (28 loc) · 1.02 KB
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
/**
* LeetCode problem 2269. Find the K-Beauty of a Number: https://leetcode.com/problems/find-the-k-beauty-of-a-number/
*/
public class Solution {
/**
* Counts the number of substrings of num which have length of k and is a divisor of num
* Time Complexity: O(N), where N = the number of digits of num
* Space Complexity: O(1)
*
* @param num the integer to find the k-beauty of
* @param k
* @return the k-beauty of the number
*/
public int divisorSubstrings(int num, int k) {
// count number of substrings of num which have length of k, divisor of num
int count = 0;
String numStr = Integer.toString(num);
for (int i = 0; i < numStr.length() + 1 - k; i++) {
// substring always has length of k
int numSub = Integer.parseInt(numStr.substring(i, i + k));
// don't compute if the substring is 0
if (numSub != 0 && num % numSub == 0) {
count += 1;
}
}
return count;
}
}