diff --git a/src/main/kotlin/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/Solution.kt b/src/main/kotlin/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/Solution.kt new file mode 100644 index 000000000..d089e705e --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/Solution.kt @@ -0,0 +1,25 @@ +package g3201_3300.s3300_minimum_element_after_replacement_with_digit_sum + +// #Easy #Array #Math #2024_10_01_Time_153_ms_(100.00%)_Space_36.5_MB_(95.24%) + +import kotlin.math.min + +class Solution { + fun minElement(nums: IntArray): Int { + var min = Int.Companion.MAX_VALUE + for (x in nums) { + min = min(min, solve(x)) + } + return min + } + + private fun solve(x: Int): Int { + var x = x + var sum = 0 + while (x != 0) { + sum += x % 10 + x /= 10 + } + return sum + } +} diff --git a/src/main/kotlin/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/readme.md b/src/main/kotlin/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/readme.md new file mode 100644 index 000000000..559b822eb --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/readme.md @@ -0,0 +1,44 @@ +3300\. Minimum Element After Replacement With Digit Sum + +Easy + +You are given an integer array `nums`. + +You replace each element in `nums` with the **sum** of its digits. + +Return the **minimum** element in `nums` after all replacements. + +**Example 1:** + +**Input:** nums = [10,12,13,14] + +**Output:** 1 + +**Explanation:** + +`nums` becomes `[1, 3, 4, 5]` after all replacements, with minimum element 1. + +**Example 2:** + +**Input:** nums = [1,2,3,4] + +**Output:** 1 + +**Explanation:** + +`nums` becomes `[1, 2, 3, 4]` after all replacements, with minimum element 1. + +**Example 3:** + +**Input:** nums = [999,19,199] + +**Output:** 10 + +**Explanation:** + +`nums` becomes `[27, 10, 19]` after all replacements, with minimum element 10. + +**Constraints:** + +* `1 <= nums.length <= 100` +* 1 <= nums[i] <= 104 \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/Solution.kt b/src/main/kotlin/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/Solution.kt new file mode 100644 index 000000000..68fea7cf6 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/Solution.kt @@ -0,0 +1,25 @@ +package g3301_3400.s3301_maximize_the_total_height_of_unique_towers + +// #Medium #Array #Sorting #Greedy #2024_10_01_Time_761_ms_(87.50%)_Space_68.1_MB_(77.50%) + +class Solution { + fun maximumTotalSum(maximumHeight: IntArray): Long { + maximumHeight.sort() + var result = maximumHeight[maximumHeight.size - 1].toLong() + var previousHeight = maximumHeight[maximumHeight.size - 1].toLong() + for (i in maximumHeight.size - 2 downTo 0) { + if (previousHeight == 1L) { + return -1 + } + val height = maximumHeight[i].toLong() + if (height >= previousHeight) { + result = result + previousHeight - 1 + previousHeight = previousHeight - 1 + } else { + result = result + height + previousHeight = height + } + } + return result + } +} diff --git a/src/main/kotlin/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/readme.md b/src/main/kotlin/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/readme.md new file mode 100644 index 000000000..debc12a57 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/readme.md @@ -0,0 +1,47 @@ +3301\. Maximize the Total Height of Unique Towers + +Medium + +You are given an array `maximumHeight`, where `maximumHeight[i]` denotes the **maximum** height the ith tower can be assigned. + +Your task is to assign a height to each tower so that: + +1. The height of the ith tower is a positive integer and does not exceed `maximumHeight[i]`. +2. No two towers have the same height. + +Return the **maximum** possible total sum of the tower heights. If it's not possible to assign heights, return `-1`. + +**Example 1:** + +**Input:** maximumHeight = [2,3,4,3] + +**Output:** 10 + +**Explanation:** + +We can assign heights in the following way: `[1, 2, 4, 3]`. + +**Example 2:** + +**Input:** maximumHeight = [15,10] + +**Output:** 25 + +**Explanation:** + +We can assign heights in the following way: `[15, 10]`. + +**Example 3:** + +**Input:** maximumHeight = [2,2,1] + +**Output:** \-1 + +**Explanation:** + +It's impossible to assign positive heights to each index so that no two towers have the same height. + +**Constraints:** + +* 1 <= maximumHeight.length <= 105 +* 1 <= maximumHeight[i] <= 109 \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/Solution.kt b/src/main/kotlin/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/Solution.kt new file mode 100644 index 000000000..1b17c444c --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/Solution.kt @@ -0,0 +1,49 @@ +package g3301_3400.s3302_find_the_lexicographically_smallest_valid_sequence + +// #Medium #String #Dynamic_Programming #Greedy #Two_Pointers +// #2024_10_01_Time_705_ms_(100.00%)_Space_65.9_MB_(100.00%) + +class Solution { + fun validSequence(word1: String, word2: String): IntArray? { + val c1 = word1.toCharArray() + val c2 = word2.toCharArray() + val dp = IntArray(c1.size + 1) + var j = c2.size - 1 + for (i in c1.indices.reversed()) { + if (j >= 0 && c1[i] == c2[j]) { + dp[i] = dp[i + 1] + 1 + j-- + } else { + dp[i] = dp[i + 1] + } + } + val ans = IntArray(c2.size) + var i = 0 + j = 0 + while (i < c1.size && j < c2.size) { + if (c1[i] == c2[j]) { + ans[j] = i + j++ + } else { + if (dp[i + 1] >= c2.size - 1 - j) { + ans[j] = i + j++ + i++ + break + } + } + i++ + } + if (j < c2.size && i == c1.size) { + return IntArray(0) + } + while (j < c2.size && i < c1.size) { + if (c2[j] == c1[i]) { + ans[j] = i + j++ + } + i++ + } + return ans + } +} diff --git a/src/main/kotlin/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/readme.md b/src/main/kotlin/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/readme.md new file mode 100644 index 000000000..feaa7b957 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/readme.md @@ -0,0 +1,65 @@ +3302\. Find the Lexicographically Smallest Valid Sequence + +Medium + +You are given two strings `word1` and `word2`. + +A string `x` is called **almost equal** to `y` if you can change **at most** one character in `x` to make it _identical_ to `y`. + +A sequence of indices `seq` is called **valid** if: + +* The indices are sorted in **ascending** order. +* _Concatenating_ the characters at these indices in `word1` in **the same** order results in a string that is **almost equal** to `word2`. + +Return an array of size `word2.length` representing the lexicographically smallest **valid** sequence of indices. If no such sequence of indices exists, return an **empty** array. + +**Note** that the answer must represent the _lexicographically smallest array_, **not** the corresponding string formed by those indices. + +**Example 1:** + +**Input:** word1 = "vbcca", word2 = "abc" + +**Output:** [0,1,2] + +**Explanation:** + +The lexicographically smallest valid sequence of indices is `[0, 1, 2]`: + +* Change `word1[0]` to `'a'`. +* `word1[1]` is already `'b'`. +* `word1[2]` is already `'c'`. + +**Example 2:** + +**Input:** word1 = "bacdc", word2 = "abc" + +**Output:** [1,2,4] + +**Explanation:** + +The lexicographically smallest valid sequence of indices is `[1, 2, 4]`: + +* `word1[1]` is already `'a'`. +* Change `word1[2]` to `'b'`. +* `word1[4]` is already `'c'`. + +**Example 3:** + +**Input:** word1 = "aaaaaa", word2 = "aaabc" + +**Output:** [] + +**Explanation:** + +There is no valid sequence of indices. + +**Example 4:** + +**Input:** word1 = "abc", word2 = "ab" + +**Output:** [0,1] + +**Constraints:** + +* 1 <= word2.length < word1.length <= 3 * 105 +* `word1` and `word2` consist only of lowercase English letters. \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/Solution.kt b/src/main/kotlin/g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/Solution.kt new file mode 100644 index 000000000..f97afe37f --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/Solution.kt @@ -0,0 +1,60 @@ +package g3301_3400.s3303_find_the_occurrence_of_first_almost_equal_substring + +// #Hard #String #String_Matching #2024_10_01_Time_364_ms_(100.00%)_Space_40.8_MB_(100.00%) + +import kotlin.math.abs + +class Solution { + fun minStartingIndex(s: String, pattern: String): Int { + val n = s.length + var left = 0 + var right = 0 + val f1 = IntArray(26) + val f2 = IntArray(26) + for (ch in pattern.toCharArray()) { + f2[ch.code - 'a'.code]++ + } + while (right < n) { + val ch = s[right] + f1[ch.code - 'a'.code]++ + if (right - left + 1 == pattern.length + 1) { + f1[s[left].code - 'a'.code]-- + left += 1 + } + if (right - left + 1 == pattern.length && check(f1, f2, left, s, pattern)) { + return left + } + right += 1 + } + return -1 + } + + private fun check(f1: IntArray, f2: IntArray, left: Int, s: String, pattern: String): Boolean { + var cnt = 0 + for (i in 0..25) { + if (f1[i] != f2[i]) { + if ((abs((f1[i] - f2[i])) > 1) || (abs(f1[i] - f2[i]) != 1 && cnt == 2)) { + return false + } + cnt += 1 + } + } + cnt = 0 + var start = 0 + var end = pattern.length - 1 + while (start <= end) { + if (s[start + left] != pattern[start]) { + cnt += 1 + } + if (start + left != left + end && s[left + end] != pattern[end]) { + cnt += 1 + } + if (cnt >= 2) { + return false + } + start++ + end-- + } + return true + } +} diff --git a/src/main/kotlin/g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/readme.md b/src/main/kotlin/g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/readme.md new file mode 100644 index 000000000..bf26fc01e --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/readme.md @@ -0,0 +1,50 @@ +3303\. Find the Occurrence of First Almost Equal Substring + +Hard + +You are given two strings `s` and `pattern`. + +A string `x` is called **almost equal** to `y` if you can change **at most** one character in `x` to make it _identical_ to `y`. + +Return the **smallest** _starting index_ of a substring in `s` that is **almost equal** to `pattern`. If no such index exists, return `-1`. + +A **substring** is a contiguous **non-empty** sequence of characters within a string. + +**Example 1:** + +**Input:** s = "abcdefg", pattern = "bcdffg" + +**Output:** 1 + +**Explanation:** + +The substring `s[1..6] == "bcdefg"` can be converted to `"bcdffg"` by changing `s[4]` to `"f"`. + +**Example 2:** + +**Input:** s = "ababbababa", pattern = "bacaba" + +**Output:** 4 + +**Explanation:** + +The substring `s[4..9] == "bababa"` can be converted to `"bacaba"` by changing `s[6]` to `"c"`. + +**Example 3:** + +**Input:** s = "abcd", pattern = "dba" + +**Output:** \-1 + +**Example 4:** + +**Input:** s = "dde", pattern = "d" + +**Output:** 0 + +**Constraints:** + +* 1 <= pattern.length < s.length <= 3 * 105 +* `s` and `pattern` consist only of lowercase English letters. + +**Follow-up:** Could you solve the problem if **at most** `k` **consecutive** characters can be changed? \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3304_find_the_k_th_character_in_string_game_i/Solution.kt b/src/main/kotlin/g3301_3400/s3304_find_the_k_th_character_in_string_game_i/Solution.kt new file mode 100644 index 000000000..23dc631ea --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3304_find_the_k_th_character_in_string_game_i/Solution.kt @@ -0,0 +1,34 @@ +package g3301_3400.s3304_find_the_k_th_character_in_string_game_i + +// #Easy #Math #Bit_Manipulation #Simulation #Recursion +// #2024_10_01_Time_140_ms_(96.43%)_Space_33.8_MB_(100.00%) + +class Solution { + fun kthCharacter(k: Int): Char { + // Initialize the length of the current string + // Initial length when word = "a" + var k = k + var length = 1 + // Find the total length after enough iterations + while (length < k) { + length *= 2 + } + // Trace back to the original character + // Start with 'a' + var currentChar = 'a' + while (length > 1) { + length /= 2 + if (k > length) { + // Adjust k for the next character + k -= length + // Move to the next character + currentChar++ + if (currentChar > 'z') { + // Wrap around if exceeds 'z' + currentChar = 'a' + } + } + } + return currentChar + } +} diff --git a/src/main/kotlin/g3301_3400/s3304_find_the_k_th_character_in_string_game_i/readme.md b/src/main/kotlin/g3301_3400/s3304_find_the_k_th_character_in_string_game_i/readme.md new file mode 100644 index 000000000..e7bf1bae0 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3304_find_the_k_th_character_in_string_game_i/readme.md @@ -0,0 +1,41 @@ +3304\. Find the K-th Character in String Game I + +Easy + +Alice and Bob are playing a game. Initially, Alice has a string `word = "a"`. + +You are given a **positive** integer `k`. + +Now Bob will ask Alice to perform the following operation **forever**: + +* Generate a new string by **changing** each character in `word` to its **next** character in the English alphabet, and **append** it to the _original_ `word`. + +For example, performing the operation on `"c"` generates `"cd"` and performing the operation on `"zb"` generates `"zbac"`. + +Return the value of the kth character in `word`, after enough operations have been done for `word` to have **at least** `k` characters. + +**Note** that the character `'z'` can be changed to `'a'` in the operation. + +**Example 1:** + +**Input:** k = 5 + +**Output:** "b" + +**Explanation:** + +Initially, `word = "a"`. We need to do the operation three times: + +* Generated string is `"b"`, `word` becomes `"ab"`. +* Generated string is `"bc"`, `word` becomes `"abbc"`. +* Generated string is `"bccd"`, `word` becomes `"abbcbccd"`. + +**Example 2:** + +**Input:** k = 10 + +**Output:** "c" + +**Constraints:** + +* `1 <= k <= 500` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i/Solution.kt b/src/main/kotlin/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i/Solution.kt new file mode 100644 index 000000000..585c1c213 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i/Solution.kt @@ -0,0 +1,58 @@ +package g3301_3400.s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i + +// #Medium #String #Hash_Table #Sliding_Window +// #2024_10_01_Time_153_ms_(100.00%)_Space_35.1_MB_(100.00%) + +class Solution { + fun countOfSubstrings(word: String, k: Int): Int { + val arr = word.toCharArray() + val map = IntArray(26) + map[0]++ + map['e'.code - 'a'.code]++ + map['i'.code - 'a'.code]++ + map['o'.code - 'a'.code]++ + map['u'.code - 'a'.code]++ + var need = 5 + var ans = 0 + var consCnt = 0 + var j = 0 + for (i in arr.indices) { + while (j < arr.size && (need > 0 || consCnt < k)) { + if (isVowel(arr[j])) { + map[arr[j].code - 'a'.code]-- + if (map[arr[j].code - 'a'.code] == 0) { + need-- + } + } else { + consCnt++ + } + j++ + } + if (need == 0 && consCnt == k) { + ans++ + var m = j + while (m < arr.size) { + if (isVowel(arr[m])) { + ans++ + } else { + break + } + m++ + } + } + if (isVowel(arr[i])) { + map[arr[i].code - 'a'.code]++ + if (map[arr[i].code - 'a'.code] == 1) { + need++ + } + } else { + consCnt-- + } + } + return ans + } + + private fun isVowel(ch: Char): Boolean { + return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' + } +} diff --git a/src/main/kotlin/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i/readme.md b/src/main/kotlin/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i/readme.md new file mode 100644 index 000000000..437079055 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i/readme.md @@ -0,0 +1,47 @@ +3305\. Count of Substrings Containing Every Vowel and K Consonants I + +Medium + +You are given a string `word` and a **non-negative** integer `k`. + +Return the total number of substrings of `word` that contain every vowel (`'a'`, `'e'`, `'i'`, `'o'`, and `'u'`) **at least** once and **exactly** `k` consonants. + +**Example 1:** + +**Input:** word = "aeioqq", k = 1 + +**Output:** 0 + +**Explanation:** + +There is no substring with every vowel. + +**Example 2:** + +**Input:** word = "aeiou", k = 0 + +**Output:** 1 + +**Explanation:** + +The only substring with every vowel and zero consonants is `word[0..4]`, which is `"aeiou"`. + +**Example 3:** + +**Input:** word = "ieaouqqieaouqq", k = 1 + +**Output:** 3 + +**Explanation:** + +The substrings with every vowel and one consonant are: + +* `word[0..5]`, which is `"ieaouq"`. +* `word[6..11]`, which is `"qieaou"`. +* `word[7..12]`, which is `"ieaouq"`. + +**Constraints:** + +* `5 <= word.length <= 250` +* `word` consists only of lowercase English letters. +* `0 <= k <= word.length - 5` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/Solution.kt b/src/main/kotlin/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/Solution.kt new file mode 100644 index 000000000..03403cebb --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/Solution.kt @@ -0,0 +1,61 @@ +package g3301_3400.s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii + +// #Medium #String #Hash_Table #Sliding_Window +// #2024_10_01_Time_651_ms_(100.00%)_Space_54.9_MB_(40.00%) + +import java.util.HashMap +import java.util.HashSet + +class Solution { + fun countOfSubstrings(word: String, k: Int): Long { + return ( + countOfSubstringHavingAtleastXConsonants(word, k) - + countOfSubstringHavingAtleastXConsonants(word, k + 1) + ) + } + + private fun countOfSubstringHavingAtleastXConsonants(word: String, k: Int): Long { + var start = 0 + var end = 0 + val vowels: MutableSet = HashSet() + vowels.add('a') + vowels.add('e') + vowels.add('i') + vowels.add('o') + vowels.add('u') + var consonants = 0 + val map: MutableMap = HashMap() + var res: Long = 0 + while (end < word.length) { + val ch = word[end] + // adding vowel or consonants; + if (vowels.contains(ch)) { + if (map.containsKey(ch)) { + map.put(ch, map[ch]!! + 1) + } else { + map.put(ch, 1) + } + } else { + consonants++ + } + // checking any valid string ispresent or not + while (map.size == 5 && consonants >= k) { + res += (word.length - end).toLong() + val ch1 = word[start] + if (vowels.contains(ch1)) { + val temp = map[ch1]!! - 1 + if (temp == 0) { + map.remove(ch1) + } else { + map.put(ch1, temp) + } + } else { + consonants-- + } + start++ + } + end++ + } + return res + } +} diff --git a/src/main/kotlin/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/readme.md b/src/main/kotlin/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/readme.md new file mode 100644 index 000000000..d0d4db072 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/readme.md @@ -0,0 +1,47 @@ +3306\. Count of Substrings Containing Every Vowel and K Consonants II + +Medium + +You are given a string `word` and a **non-negative** integer `k`. + +Return the total number of substrings of `word` that contain every vowel (`'a'`, `'e'`, `'i'`, `'o'`, and `'u'`) **at least** once and **exactly** `k` consonants. + +**Example 1:** + +**Input:** word = "aeioqq", k = 1 + +**Output:** 0 + +**Explanation:** + +There is no substring with every vowel. + +**Example 2:** + +**Input:** word = "aeiou", k = 0 + +**Output:** 1 + +**Explanation:** + +The only substring with every vowel and zero consonants is `word[0..4]`, which is `"aeiou"`. + +**Example 3:** + +**Input:** word = "ieaouqqieaouqq", k = 1 + +**Output:** 3 + +**Explanation:** + +The substrings with every vowel and one consonant are: + +* `word[0..5]`, which is `"ieaouq"`. +* `word[6..11]`, which is `"qieaou"`. +* `word[7..12]`, which is `"ieaouq"`. + +**Constraints:** + +* 5 <= word.length <= 2 * 105 +* `word` consists only of lowercase English letters. +* `0 <= k <= word.length - 5` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii/Solution.kt b/src/main/kotlin/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii/Solution.kt new file mode 100644 index 000000000..ffd21e94d --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii/Solution.kt @@ -0,0 +1,27 @@ +package g3301_3400.s3307_find_the_k_th_character_in_string_game_ii + +// #Hard #Math #Bit_Manipulation #Recursion #2024_10_01_Time_189_ms_(50.00%)_Space_36.7_MB_(83.33%) + +class Solution { + fun kthCharacter(k: Long, operations: IntArray): Char { + if (k == 1L) { + return 'a' + } + var len: Long = 1 + var newK: Long = -1 + var operation = -1 + for (ope in operations) { + len *= 2 + if (len >= k) { + operation = ope + newK = k - len / 2 + break + } + } + val ch = kthCharacter(newK, operations) + if (operation == 0) { + return ch + } + return if (ch == 'z') 'a' else (ch.code + 1).toChar() + } +} diff --git a/src/main/kotlin/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii/readme.md b/src/main/kotlin/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii/readme.md new file mode 100644 index 000000000..cf95c751e --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii/readme.md @@ -0,0 +1,52 @@ +3307\. Find the K-th Character in String Game II + +Hard + +Alice and Bob are playing a game. Initially, Alice has a string `word = "a"`. + +You are given a **positive** integer `k`. You are also given an integer array `operations`, where `operations[i]` represents the **type** of the ith operation. + +Now Bob will ask Alice to perform **all** operations in sequence: + +* If `operations[i] == 0`, **append** a copy of `word` to itself. +* If `operations[i] == 1`, generate a new string by **changing** each character in `word` to its **next** character in the English alphabet, and **append** it to the _original_ `word`. For example, performing the operation on `"c"` generates `"cd"` and performing the operation on `"zb"` generates `"zbac"`. + +Return the value of the kth character in `word` after performing all the operations. + +**Note** that the character `'z'` can be changed to `'a'` in the second type of operation. + +**Example 1:** + +**Input:** k = 5, operations = [0,0,0] + +**Output:** "a" + +**Explanation:** + +Initially, `word == "a"`. Alice performs the three operations as follows: + +* Appends `"a"` to `"a"`, `word` becomes `"aa"`. +* Appends `"aa"` to `"aa"`, `word` becomes `"aaaa"`. +* Appends `"aaaa"` to `"aaaa"`, `word` becomes `"aaaaaaaa"`. + +**Example 2:** + +**Input:** k = 10, operations = [0,1,0,1] + +**Output:** "b" + +**Explanation:** + +Initially, `word == "a"`. Alice performs the four operations as follows: + +* Appends `"a"` to `"a"`, `word` becomes `"aa"`. +* Appends `"bb"` to `"aa"`, `word` becomes `"aabb"`. +* Appends `"aabb"` to `"aabb"`, `word` becomes `"aabbaabb"`. +* Appends `"bbccbbcc"` to `"aabbaabb"`, `word` becomes `"aabbaabbbbccbbcc"`. + +**Constraints:** + +* 1 <= k <= 1014 +* `1 <= operations.length <= 100` +* `operations[i]` is either 0 or 1. +* The input is generated such that `word` has **at least** `k` characters after all operations. \ No newline at end of file diff --git a/src/test/kotlin/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/SolutionTest.kt new file mode 100644 index 000000000..4e216822e --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3201_3300.s3300_minimum_element_after_replacement_with_digit_sum + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minElement() { + assertThat(Solution().minElement(intArrayOf(10, 12, 13, 14)), equalTo(1)) + } + + @Test + fun minElement2() { + assertThat(Solution().minElement(intArrayOf(1, 2, 3, 4)), equalTo(1)) + } + + @Test + fun minElement3() { + assertThat(Solution().minElement(intArrayOf(999, 19, 199)), equalTo(10)) + } +} diff --git a/src/test/kotlin/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/SolutionTest.kt new file mode 100644 index 000000000..c82c8c7b5 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/SolutionTest.kt @@ -0,0 +1,31 @@ +package g3301_3400.s3301_maximize_the_total_height_of_unique_towers + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maximumTotalSum() { + assertThat( + Solution().maximumTotalSum(intArrayOf(2, 3, 4, 3)), + equalTo(10L) + ) + } + + @Test + fun maximumTotalSum2() { + assertThat( + Solution().maximumTotalSum(intArrayOf(15, 10)), + equalTo(25L) + ) + } + + @Test + fun maximumTotalSum3() { + assertThat( + Solution().maximumTotalSum(intArrayOf(2, 2, 1)), + equalTo(-1L) + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/SolutionTest.kt new file mode 100644 index 000000000..8c6d1bfd0 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/SolutionTest.kt @@ -0,0 +1,39 @@ +package g3301_3400.s3302_find_the_lexicographically_smallest_valid_sequence + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun validSequence() { + assertThat( + Solution().validSequence("vbcca", "abc"), + equalTo(intArrayOf(0, 1, 2)) + ) + } + + @Test + fun validSequence2() { + assertThat( + Solution().validSequence("bacdc", "abc"), + equalTo(intArrayOf(1, 2, 4)) + ) + } + + @Test + fun validSequence3() { + assertThat( + Solution().validSequence("aaaaaa", "aaabc"), + equalTo(intArrayOf()) + ) + } + + @Test + fun validSequence4() { + assertThat( + Solution().validSequence("abc", "ab"), + equalTo(intArrayOf(0, 1)) + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/SolutionTest.kt new file mode 100644 index 000000000..1c2dbe3bf --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/SolutionTest.kt @@ -0,0 +1,30 @@ +package g3301_3400.s3303_find_the_occurrence_of_first_almost_equal_substring + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minStartingIndex() { + assertThat(Solution().minStartingIndex("abcdefg", "bcdffg"), equalTo(1)) + } + + @Test + fun minStartingIndex2() { + assertThat( + Solution().minStartingIndex("ababbababa", "bacaba"), + equalTo(4) + ) + } + + @Test + fun minStartingIndex3() { + assertThat(Solution().minStartingIndex("abcd", "dba"), equalTo(-1)) + } + + @Test + fun minStartingIndex4() { + assertThat(Solution().minStartingIndex("dde", "d"), equalTo(0)) + } +} diff --git a/src/test/kotlin/g3301_3400/s3304_find_the_k_th_character_in_string_game_i/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3304_find_the_k_th_character_in_string_game_i/SolutionTest.kt new file mode 100644 index 000000000..b41279913 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3304_find_the_k_th_character_in_string_game_i/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3301_3400.s3304_find_the_k_th_character_in_string_game_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun kthCharacter() { + assertThat(Solution().kthCharacter(5), equalTo('b')) + } + + @Test + fun kthCharacter2() { + assertThat(Solution().kthCharacter(10), equalTo('c')) + } +} diff --git a/src/test/kotlin/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i/SolutionTest.kt new file mode 100644 index 000000000..e8f5e7152 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3301_3400.s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun countOfSubstrings() { + assertThat(Solution().countOfSubstrings("aeioqq", 1), equalTo(0)) + } + + @Test + fun countOfSubstrings2() { + assertThat(Solution().countOfSubstrings("aeiou", 0), equalTo(1)) + } + + @Test + fun countOfSubstrings3() { + assertThat(Solution().countOfSubstrings("ieaouqqieaouqq", 1), equalTo(3)) + } +} diff --git a/src/test/kotlin/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/SolutionTest.kt new file mode 100644 index 000000000..0cf52341c --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/SolutionTest.kt @@ -0,0 +1,25 @@ +package g3301_3400.s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun countOfSubstrings() { + assertThat(Solution().countOfSubstrings("aeioqq", 1), equalTo(0L)) + } + + @Test + fun countOfSubstrings2() { + assertThat(Solution().countOfSubstrings("aeiou", 0), equalTo(1L)) + } + + @Test + fun countOfSubstrings3() { + assertThat( + Solution().countOfSubstrings("ieaouqqieaouqq", 1), + equalTo(3L) + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii/SolutionTest.kt new file mode 100644 index 000000000..f1faea789 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3301_3400.s3307_find_the_k_th_character_in_string_game_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun kthCharacter() { + assertThat( + Solution().kthCharacter(5, intArrayOf(0, 0, 0)), + equalTo('a') + ) + } + + @Test + fun kthCharacter2() { + assertThat( + Solution().kthCharacter(10, intArrayOf(0, 1, 0, 1)), + equalTo('b') + ) + } +}