Skip to content

Commit 86c9908

Browse files
authored
Added tasks 3136-3139
1 parent b83c690 commit 86c9908

File tree

12 files changed

+453
-0
lines changed

12 files changed

+453
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g3101_3200.s3136_valid_word
2+
3+
// #Easy #String #2024_05_07_Time_160_ms_(92.50%)_Space_35.5_MB_(90.00%)
4+
5+
class Solution {
6+
fun isValid(word: String): Boolean {
7+
if (word.length < 3) {
8+
return false
9+
}
10+
if (word.contains("@") || word.contains("#") || word.contains("$")) {
11+
return false
12+
}
13+
val vowels = charArrayOf('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')
14+
val consonants = charArrayOf(
15+
'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v',
16+
'w', 'x', 'y', 'z', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q',
17+
'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z'
18+
)
19+
var flag1 = false
20+
var flag2 = false
21+
for (c in vowels) {
22+
if (word.indexOf(c) != -1) {
23+
flag1 = true
24+
break
25+
}
26+
}
27+
for (c in consonants) {
28+
if (word.indexOf(c) != -1) {
29+
flag2 = true
30+
break
31+
}
32+
}
33+
return flag1 && flag2
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
3136\. Valid Word
2+
3+
Easy
4+
5+
A word is considered **valid** if:
6+
7+
* It contains a **minimum** of 3 characters.
8+
* It contains only digits (0-9), and English letters (uppercase and lowercase).
9+
* It includes **at least** one **vowel**.
10+
* It includes **at least** one **consonant**.
11+
12+
You are given a string `word`.
13+
14+
Return `true` if `word` is valid, otherwise, return `false`.
15+
16+
**Notes:**
17+
18+
* `'a'`, `'e'`, `'i'`, `'o'`, `'u'`, and their uppercases are **vowels**.
19+
* A **consonant** is an English letter that is not a vowel.
20+
21+
**Example 1:**
22+
23+
**Input:** word = "234Adas"
24+
25+
**Output:** true
26+
27+
**Explanation:**
28+
29+
This word satisfies the conditions.
30+
31+
**Example 2:**
32+
33+
**Input:** word = "b3"
34+
35+
**Output:** false
36+
37+
**Explanation:**
38+
39+
The length of this word is fewer than 3, and does not have a vowel.
40+
41+
**Example 3:**
42+
43+
**Input:** word = "a3$e"
44+
45+
**Output:** false
46+
47+
**Explanation:**
48+
49+
This word contains a `'$'` character and does not have a consonant.
50+
51+
**Constraints:**
52+
53+
* `1 <= word.length <= 20`
54+
* `word` consists of English uppercase and lowercase letters, digits, `'@'`, `'#'`, and `'$'`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g3101_3200.s3137_minimum_number_of_operations_to_make_word_k_periodic
2+
3+
// #Medium #String #Hash_Table #Counting #2024_05_07_Time_291_ms_(96.30%)_Space_38.8_MB_(100.00%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun minimumOperationsToMakeKPeriodic(word: String, k: Int): Int {
9+
val map: MutableMap<Int, Int> = HashMap()
10+
val n = word.length
11+
var max = 0
12+
var i = 0
13+
while (i < n) {
14+
var hash = 0
15+
for (j in i until i + k) {
16+
val idx = word[j].code - 'a'.code
17+
hash = hash * 26 + idx
18+
}
19+
var count = map.getOrDefault(hash, 0)
20+
count++
21+
map[hash] = count
22+
max = max(max, count)
23+
i += k
24+
}
25+
return n / k - max
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
3137\. Minimum Number of Operations to Make Word K-Periodic
2+
3+
Medium
4+
5+
You are given a string `word` of size `n`, and an integer `k` such that `k` divides `n`.
6+
7+
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]`.
8+
9+
Return _the **minimum** number of operations required to make_ `word` _**k-periodic**_.
10+
11+
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"`.
12+
13+
**Example 1:**
14+
15+
**Input:** word = "leetcodeleet", k = 4
16+
17+
**Output:** 1
18+
19+
**Explanation:**
20+
21+
We can obtain a 4-periodic string by picking i = 4 and j = 0. After this operation, word becomes equal to "leetleetleet".
22+
23+
**Example 2:**
24+
25+
**Input:** word = "leetcoleet", k = 2
26+
27+
**Output:** 3
28+
29+
**Explanation:**
30+
31+
We can obtain a 2-periodic string by applying the operations in the table below.
32+
33+
i j word
34+
0 2 etetcoleet
35+
4 0 etetetleet
36+
6 0 etetetetet
37+
38+
**Constraints:**
39+
40+
* <code>1 <= n == word.length <= 10<sup>5</sup></code>
41+
* `1 <= k <= word.length`
42+
* `k` divides `word.length`.
43+
* `word` consists only of lowercase English letters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g3101_3200.s3138_minimum_length_of_anagram_concatenation
2+
3+
// #Medium #String #Hash_Table #Counting #2024_05_07_Time_219_ms_(91.67%)_Space_38.8_MB_(58.33%)
4+
5+
import kotlin.math.sqrt
6+
7+
class Solution {
8+
fun minAnagramLength(s: String): Int {
9+
val n = s.length
10+
val sq = IntArray(n)
11+
for (i in s.indices) {
12+
val ch = s[i].code
13+
if (i == 0) {
14+
sq[i] = ch * ch
15+
} else {
16+
sq[i] = sq[i - 1] + ch * ch
17+
}
18+
}
19+
val factors = getAllFactorsVer2(n)
20+
factors.sort()
21+
for (j in factors.indices) {
22+
val factor = factors[j]
23+
if (factor == 1) {
24+
if (sq[0] * n == sq[n - 1]) {
25+
return 1
26+
}
27+
} else {
28+
val sum = sq[factor - 1]
29+
var start = 0
30+
var i = factor - 1
31+
while (i < n) {
32+
if (start + sum != sq[i]) {
33+
break
34+
}
35+
start += sum
36+
if (i == n - 1) {
37+
return factor
38+
}
39+
i += factor
40+
}
41+
}
42+
}
43+
return n - 1
44+
}
45+
46+
private fun getAllFactorsVer2(n: Int): MutableList<Int> {
47+
val factors: MutableList<Int> = ArrayList()
48+
var i = 1
49+
while (i <= sqrt(n.toDouble())) {
50+
if (n % i == 0) {
51+
factors.add(i)
52+
factors.add(n / i)
53+
}
54+
i++
55+
}
56+
return factors
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
3138\. Minimum Length of Anagram Concatenation
2+
3+
Medium
4+
5+
You are given a string `s`, which is known to be a concatenation of **anagrams** of some string `t`.
6+
7+
Return the **minimum** possible length of the string `t`.
8+
9+
An **anagram** is formed by rearranging the letters of a string. For example, "aab", "aba", and, "baa" are anagrams of "aab".
10+
11+
**Example 1:**
12+
13+
**Input:** s = "abba"
14+
15+
**Output:** 2
16+
17+
**Explanation:**
18+
19+
One possible string `t` could be `"ba"`.
20+
21+
**Example 2:**
22+
23+
**Input:** s = "cdef"
24+
25+
**Output:** 4
26+
27+
**Explanation:**
28+
29+
One possible string `t` could be `"cdef"`, notice that `t` can be equal to `s`.
30+
31+
**Constraints:**
32+
33+
* <code>1 <= s.length <= 10<sup>5</sup></code>
34+
* `s` consist only of lowercase English letters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g3101_3200.s3139_minimum_cost_to_equalize_array
2+
3+
// #Hard #Array #Greedy #Enumeration #2024_05_07_Time_495_ms_(100.00%)_Space_60.4_MB_(100.00%)
4+
5+
import kotlin.math.max
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun minCostToEqualizeArray(nums: IntArray, cost1: Int, cost2: Int): Int {
10+
var max = 0L
11+
var min = Long.MAX_VALUE
12+
var sum = 0L
13+
for (num in nums) {
14+
if (num > max) {
15+
max = num.toLong()
16+
}
17+
if (num < min) {
18+
min = num.toLong()
19+
}
20+
sum += num
21+
}
22+
val n = nums.size
23+
var total = max * n - sum
24+
// When operation one is always better:
25+
if ((cost1 shl 1) <= cost2 || n <= 2) {
26+
return (total * cost1 % LMOD).toInt()
27+
}
28+
// When operation two is moderately better:
29+
var op1 = max(0L, (((max - min) shl 1L.toInt()) - total))
30+
var op2 = total - op1
31+
var result = (op1 + (op2 and 1L)) * cost1 + (op2 shr 1L.toInt()) * cost2
32+
// When operation two is significantly better:
33+
total += op1 / (n - 2L) * n
34+
op1 %= n - 2L
35+
op2 = total - op1
36+
result = min(result, ((op1 + (op2 and 1L)) * cost1 + (op2 shr 1L.toInt()) * cost2))
37+
// When operation two is always better:
38+
for (i in 0..1) {
39+
total += n.toLong()
40+
result = min(result, ((total and 1L) * cost1 + (total shr 1L.toInt()) * cost2))
41+
}
42+
return (result % LMOD).toInt()
43+
}
44+
45+
companion object {
46+
private const val MOD = 1000000007
47+
private const val LMOD = MOD.toLong()
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
3139\. Minimum Cost to Equalize Array
2+
3+
Hard
4+
5+
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:
6+
7+
* Choose an index `i` from `nums` and **increase** `nums[i]` by `1` for a cost of `cost1`.
8+
* Choose two **different** indices `i`, `j`, from `nums` and **increase** `nums[i]` and `nums[j]` by `1` for a cost of `cost2`.
9+
10+
Return the **minimum** **cost** required to make all elements in the array **equal**_._
11+
12+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [4,1], cost1 = 5, cost2 = 2
17+
18+
**Output:** 15
19+
20+
**Explanation:**
21+
22+
The following operations can be performed to make the values equal:
23+
24+
* Increase `nums[1]` by 1 for a cost of 5. `nums` becomes `[4,2]`.
25+
* Increase `nums[1]` by 1 for a cost of 5. `nums` becomes `[4,3]`.
26+
* Increase `nums[1]` by 1 for a cost of 5. `nums` becomes `[4,4]`.
27+
28+
The total cost is 15.
29+
30+
**Example 2:**
31+
32+
**Input:** nums = [2,3,3,3,5], cost1 = 2, cost2 = 1
33+
34+
**Output:** 6
35+
36+
**Explanation:**
37+
38+
The following operations can be performed to make the values equal:
39+
40+
* Increase `nums[0]` and `nums[1]` by 1 for a cost of 1. `nums` becomes `[3,4,3,3,5]`.
41+
* Increase `nums[0]` and `nums[2]` by 1 for a cost of 1. `nums` becomes `[4,4,4,3,5]`.
42+
* Increase `nums[0]` and `nums[3]` by 1 for a cost of 1. `nums` becomes `[5,4,4,4,5]`.
43+
* Increase `nums[1]` and `nums[2]` by 1 for a cost of 1. `nums` becomes `[5,5,5,4,5]`.
44+
* Increase `nums[3]` by 1 for a cost of 2. `nums` becomes `[5,5,5,5,5]`.
45+
46+
The total cost is 6.
47+
48+
**Example 3:**
49+
50+
**Input:** nums = [3,5,3], cost1 = 1, cost2 = 3
51+
52+
**Output:** 4
53+
54+
**Explanation:**
55+
56+
The following operations can be performed to make the values equal:
57+
58+
* Increase `nums[0]` by 1 for a cost of 1. `nums` becomes `[4,5,3]`.
59+
* Increase `nums[0]` by 1 for a cost of 1. `nums` becomes `[5,5,3]`.
60+
* Increase `nums[2]` by 1 for a cost of 1. `nums` becomes `[5,5,4]`.
61+
* Increase `nums[2]` by 1 for a cost of 1. `nums` becomes `[5,5,5]`.
62+
63+
The total cost is 4.
64+
65+
**Constraints:**
66+
67+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
68+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
69+
* <code>1 <= cost1 <= 10<sup>6</sup></code>
70+
* <code>1 <= cost2 <= 10<sup>6</sup></code>

0 commit comments

Comments
 (0)