Skip to content

Commit ccb2708

Browse files
authored
Added tasks 3136-3149
1 parent d884d3d commit ccb2708

File tree

13 files changed

+1083
-14
lines changed
  • src/main/kotlin/g3101_3200
    • s3136_valid_word
    • s3137_minimum_number_of_operations_to_make_word_k_periodic
    • s3138_minimum_length_of_anagram_concatenation
    • s3139_minimum_cost_to_equalize_array
    • s3142_check_if_grid_satisfies_conditions
    • s3143_maximum_points_inside_the_square
    • s3144_minimum_substring_partition_of_equal_character_frequency
    • s3145_find_products_of_elements_of_big_array
    • s3146_permutation_difference_between_two_strings
    • s3147_taking_maximum_energy_from_the_mystic_dungeon
    • s3148_maximum_difference_score_in_a_grid
    • s3149_find_the_minimum_cost_array_permutation

13 files changed

+1083
-14
lines changed

README.md

Lines changed: 26 additions & 14 deletions
Large diffs are not rendered by default.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3136\. Valid Word
5+
6+
Easy
7+
8+
A word is considered **valid** if:
9+
10+
* It contains a **minimum** of 3 characters.
11+
* It contains only digits (0-9), and English letters (uppercase and lowercase).
12+
* It includes **at least** one **vowel**.
13+
* It includes **at least** one **consonant**.
14+
15+
You are given a string `word`.
16+
17+
Return `true` if `word` is valid, otherwise, return `false`.
18+
19+
**Notes:**
20+
21+
* `'a'`, `'e'`, `'i'`, `'o'`, `'u'`, and their uppercases are **vowels**.
22+
* A **consonant** is an English letter that is not a vowel.
23+
24+
**Example 1:**
25+
26+
**Input:** word = "234Adas"
27+
28+
**Output:** true
29+
30+
**Explanation:**
31+
32+
This word satisfies the conditions.
33+
34+
**Example 2:**
35+
36+
**Input:** word = "b3"
37+
38+
**Output:** false
39+
40+
**Explanation:**
41+
42+
The length of this word is fewer than 3, and does not have a vowel.
43+
44+
**Example 3:**
45+
46+
**Input:** word = "a3$e"
47+
48+
**Output:** false
49+
50+
**Explanation:**
51+
52+
This word contains a `'$'` character and does not have a consonant.
53+
54+
**Constraints:**
55+
56+
* `1 <= word.length <= 20`
57+
* `word` consists of English uppercase and lowercase letters, digits, `'@'`, `'#'`, and `'$'`.
58+
59+
## Solution
60+
61+
```kotlin
62+
class Solution {
63+
fun isValid(word: String): Boolean {
64+
if (word.length < 3) {
65+
return false
66+
}
67+
if (word.contains("@") || word.contains("#") || word.contains("$")) {
68+
return false
69+
}
70+
val vowels = charArrayOf('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')
71+
val consonants = charArrayOf(
72+
'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v',
73+
'w', 'x', 'y', 'z', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q',
74+
'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z'
75+
)
76+
var flag1 = false
77+
var flag2 = false
78+
for (c in vowels) {
79+
if (word.indexOf(c) != -1) {
80+
flag1 = true
81+
break
82+
}
83+
}
84+
for (c in consonants) {
85+
if (word.indexOf(c) != -1) {
86+
flag2 = true
87+
break
88+
}
89+
}
90+
return flag1 && flag2
91+
}
92+
}
93+
```
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3137\. Minimum Number of Operations to Make Word K-Periodic
5+
6+
Medium
7+
8+
You are given a string `word` of size `n`, and an integer `k` such that `k` divides `n`.
9+
10+
In one operation, you can pick any two indices `i` and `j`, that are divisible by `k`, then replace the substring of length `k` starting at `i` with the substring of length `k` starting at `j`. That is, replace the substring `word[i..i + k - 1]` with the substring `word[j..j + k - 1]`.
11+
12+
Return _the **minimum** number of operations required to make_ `word` _**k-periodic**_.
13+
14+
We say that `word` is **k-periodic** if there is some string `s` of length `k` such that `word` can be obtained by concatenating `s` an arbitrary number of times. For example, if `word == “ababab”`, then `word` is 2-periodic for `s = "ab"`.
15+
16+
**Example 1:**
17+
18+
**Input:** word = "leetcodeleet", k = 4
19+
20+
**Output:** 1
21+
22+
**Explanation:**
23+
24+
We can obtain a 4-periodic string by picking i = 4 and j = 0. After this operation, word becomes equal to "leetleetleet".
25+
26+
**Example 2:**
27+
28+
**Input:** word = "leetcoleet", k = 2
29+
30+
**Output:** 3
31+
32+
**Explanation:**
33+
34+
We can obtain a 2-periodic string by applying the operations in the table below.
35+
36+
i j word
37+
0 2 etetcoleet
38+
4 0 etetetleet
39+
6 0 etetetetet
40+
41+
**Constraints:**
42+
43+
* <code>1 <= n == word.length <= 10<sup>5</sup></code>
44+
* `1 <= k <= word.length`
45+
* `k` divides `word.length`.
46+
* `word` consists only of lowercase English letters.
47+
48+
## Solution
49+
50+
```kotlin
51+
import kotlin.math.max
52+
53+
class Solution {
54+
fun minimumOperationsToMakeKPeriodic(word: String, k: Int): Int {
55+
val map: MutableMap<Int, Int> = HashMap()
56+
val n = word.length
57+
var max = 0
58+
var i = 0
59+
while (i < n) {
60+
var hash = 0
61+
for (j in i until i + k) {
62+
val idx = word[j].code - 'a'.code
63+
hash = hash * 26 + idx
64+
}
65+
var count = map.getOrDefault(hash, 0)
66+
count++
67+
map[hash] = count
68+
max = max(max, count)
69+
i += k
70+
}
71+
return n / k - max
72+
}
73+
}
74+
```
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3138\. Minimum Length of Anagram Concatenation
5+
6+
Medium
7+
8+
You are given a string `s`, which is known to be a concatenation of **anagrams** of some string `t`.
9+
10+
Return the **minimum** possible length of the string `t`.
11+
12+
An **anagram** is formed by rearranging the letters of a string. For example, "aab", "aba", and, "baa" are anagrams of "aab".
13+
14+
**Example 1:**
15+
16+
**Input:** s = "abba"
17+
18+
**Output:** 2
19+
20+
**Explanation:**
21+
22+
One possible string `t` could be `"ba"`.
23+
24+
**Example 2:**
25+
26+
**Input:** s = "cdef"
27+
28+
**Output:** 4
29+
30+
**Explanation:**
31+
32+
One possible string `t` could be `"cdef"`, notice that `t` can be equal to `s`.
33+
34+
**Constraints:**
35+
36+
* <code>1 <= s.length <= 10<sup>5</sup></code>
37+
* `s` consist only of lowercase English letters.
38+
39+
## Solution
40+
41+
```kotlin
42+
import kotlin.math.sqrt
43+
44+
class Solution {
45+
fun minAnagramLength(s: String): Int {
46+
val n = s.length
47+
val sq = IntArray(n)
48+
for (i in s.indices) {
49+
val ch = s[i].code
50+
if (i == 0) {
51+
sq[i] = ch * ch
52+
} else {
53+
sq[i] = sq[i - 1] + ch * ch
54+
}
55+
}
56+
val factors = getAllFactorsVer2(n)
57+
factors.sort()
58+
for (j in factors.indices) {
59+
val factor = factors[j]
60+
if (factor == 1) {
61+
if (sq[0] * n == sq[n - 1]) {
62+
return 1
63+
}
64+
} else {
65+
val sum = sq[factor - 1]
66+
var start = 0
67+
var i = factor - 1
68+
while (i < n) {
69+
if (start + sum != sq[i]) {
70+
break
71+
}
72+
start += sum
73+
if (i == n - 1) {
74+
return factor
75+
}
76+
i += factor
77+
}
78+
}
79+
}
80+
return n - 1
81+
}
82+
83+
private fun getAllFactorsVer2(n: Int): MutableList<Int> {
84+
val factors: MutableList<Int> = ArrayList()
85+
var i = 1
86+
while (i <= sqrt(n.toDouble())) {
87+
if (n % i == 0) {
88+
factors.add(i)
89+
factors.add(n / i)
90+
}
91+
i++
92+
}
93+
return factors
94+
}
95+
}
96+
```
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3139\. Minimum Cost to Equalize Array
5+
6+
Hard
7+
8+
You are given an integer array `nums` and two integers `cost1` and `cost2`. You are allowed to perform **either** of the following operations **any** number of times:
9+
10+
* Choose an index `i` from `nums` and **increase** `nums[i]` by `1` for a cost of `cost1`.
11+
* Choose two **different** indices `i`, `j`, from `nums` and **increase** `nums[i]` and `nums[j]` by `1` for a cost of `cost2`.
12+
13+
Return the **minimum** **cost** required to make all elements in the array **equal**_._
14+
15+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
16+
17+
**Example 1:**
18+
19+
**Input:** nums = [4,1], cost1 = 5, cost2 = 2
20+
21+
**Output:** 15
22+
23+
**Explanation:**
24+
25+
The following operations can be performed to make the values equal:
26+
27+
* Increase `nums[1]` by 1 for a cost of 5. `nums` becomes `[4,2]`.
28+
* Increase `nums[1]` by 1 for a cost of 5. `nums` becomes `[4,3]`.
29+
* Increase `nums[1]` by 1 for a cost of 5. `nums` becomes `[4,4]`.
30+
31+
The total cost is 15.
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [2,3,3,3,5], cost1 = 2, cost2 = 1
36+
37+
**Output:** 6
38+
39+
**Explanation:**
40+
41+
The following operations can be performed to make the values equal:
42+
43+
* Increase `nums[0]` and `nums[1]` by 1 for a cost of 1. `nums` becomes `[3,4,3,3,5]`.
44+
* Increase `nums[0]` and `nums[2]` by 1 for a cost of 1. `nums` becomes `[4,4,4,3,5]`.
45+
* Increase `nums[0]` and `nums[3]` by 1 for a cost of 1. `nums` becomes `[5,4,4,4,5]`.
46+
* Increase `nums[1]` and `nums[2]` by 1 for a cost of 1. `nums` becomes `[5,5,5,4,5]`.
47+
* Increase `nums[3]` by 1 for a cost of 2. `nums` becomes `[5,5,5,5,5]`.
48+
49+
The total cost is 6.
50+
51+
**Example 3:**
52+
53+
**Input:** nums = [3,5,3], cost1 = 1, cost2 = 3
54+
55+
**Output:** 4
56+
57+
**Explanation:**
58+
59+
The following operations can be performed to make the values equal:
60+
61+
* Increase `nums[0]` by 1 for a cost of 1. `nums` becomes `[4,5,3]`.
62+
* Increase `nums[0]` by 1 for a cost of 1. `nums` becomes `[5,5,3]`.
63+
* Increase `nums[2]` by 1 for a cost of 1. `nums` becomes `[5,5,4]`.
64+
* Increase `nums[2]` by 1 for a cost of 1. `nums` becomes `[5,5,5]`.
65+
66+
The total cost is 4.
67+
68+
**Constraints:**
69+
70+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
71+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
72+
* <code>1 <= cost1 <= 10<sup>6</sup></code>
73+
* <code>1 <= cost2 <= 10<sup>6</sup></code>
74+
75+
## Solution
76+
77+
```kotlin
78+
import kotlin.math.max
79+
import kotlin.math.min
80+
81+
class Solution {
82+
fun minCostToEqualizeArray(nums: IntArray, cost1: Int, cost2: Int): Int {
83+
var max = 0L
84+
var min = Long.MAX_VALUE
85+
var sum = 0L
86+
for (num in nums) {
87+
if (num > max) {
88+
max = num.toLong()
89+
}
90+
if (num < min) {
91+
min = num.toLong()
92+
}
93+
sum += num
94+
}
95+
val n = nums.size
96+
var total = max * n - sum
97+
// When operation one is always better:
98+
if ((cost1 shl 1) <= cost2 || n <= 2) {
99+
return (total * cost1 % LMOD).toInt()
100+
}
101+
// When operation two is moderately better:
102+
var op1 = max(0L, (((max - min) shl 1L.toInt()) - total))
103+
var op2 = total - op1
104+
var result = (op1 + (op2 and 1L)) * cost1 + (op2 shr 1L.toInt()) * cost2
105+
// When operation two is significantly better:
106+
total += op1 / (n - 2L) * n
107+
op1 %= n - 2L
108+
op2 = total - op1
109+
result = min(result, ((op1 + (op2 and 1L)) * cost1 + (op2 shr 1L.toInt()) * cost2))
110+
// When operation two is always better:
111+
for (i in 0..1) {
112+
total += n.toLong()
113+
result = min(result, ((total and 1L) * cost1 + (total shr 1L.toInt()) * cost2))
114+
}
115+
return (result % LMOD).toInt()
116+
}
117+
118+
companion object {
119+
private const val MOD = 1000000007
120+
private const val LMOD = MOD.toLong()
121+
}
122+
}
123+
```

0 commit comments

Comments
 (0)