From ea2860e059784942b60e2a2a5d6b26066126103f Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 1 Oct 2024 06:41:05 +0300 Subject: [PATCH] Added tasks 3300-3307 --- .../Solution.java | 22 +++++++ .../readme.md | 44 +++++++++++++ .../Solution.java | 27 ++++++++ .../readme.md | 47 ++++++++++++++ .../Solution.java | 49 ++++++++++++++ .../readme.md | 65 +++++++++++++++++++ .../Solution.java | 58 +++++++++++++++++ .../readme.md | 50 ++++++++++++++ .../Solution.java | 34 ++++++++++ .../readme.md | 41 ++++++++++++ .../Solution.java | 57 ++++++++++++++++ .../readme.md | 47 ++++++++++++++ .../Solution.java | 61 +++++++++++++++++ .../readme.md | 47 ++++++++++++++ .../Solution.java | 27 ++++++++ .../readme.md | 52 +++++++++++++++ .../SolutionTest.java | 23 +++++++ .../SolutionTest.java | 23 +++++++ .../SolutionTest.java | 28 ++++++++ .../SolutionTest.java | 28 ++++++++ .../SolutionTest.java | 18 +++++ .../SolutionTest.java | 23 +++++++ .../SolutionTest.java | 23 +++++++ .../SolutionTest.java | 18 +++++ 24 files changed, 912 insertions(+) create mode 100644 src/main/java/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/Solution.java create mode 100644 src/main/java/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/readme.md create mode 100644 src/main/java/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/Solution.java create mode 100644 src/main/java/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/readme.md create mode 100644 src/main/java/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/Solution.java create mode 100644 src/main/java/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/readme.md create mode 100644 src/main/java/g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/Solution.java create mode 100644 src/main/java/g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/readme.md create mode 100644 src/main/java/g3301_3400/s3304_find_the_k_th_character_in_string_game_i/Solution.java create mode 100644 src/main/java/g3301_3400/s3304_find_the_k_th_character_in_string_game_i/readme.md create mode 100644 src/main/java/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i/Solution.java create mode 100644 src/main/java/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i/readme.md create mode 100644 src/main/java/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/Solution.java create mode 100644 src/main/java/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/readme.md create mode 100644 src/main/java/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii/Solution.java create mode 100644 src/main/java/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii/readme.md create mode 100644 src/test/java/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3304_find_the_k_th_character_in_string_game_i/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/SolutionTest.java create mode 100644 src/test/java/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii/SolutionTest.java diff --git a/src/main/java/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/Solution.java b/src/main/java/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/Solution.java new file mode 100644 index 000000000..f66bd4d68 --- /dev/null +++ b/src/main/java/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/Solution.java @@ -0,0 +1,22 @@ +package g3201_3300.s3300_minimum_element_after_replacement_with_digit_sum; + +// #Easy #Array #Math #2024_10_01_Time_1_ms_(100.00%)_Space_42.9_MB_(75.97%) + +public class Solution { + public int minElement(int[] nums) { + int min = Integer.MAX_VALUE; + for (int x : nums) { + min = Math.min(min, solve(x)); + } + return min; + } + + private int solve(int x) { + int sum = 0; + while (x != 0) { + sum += x % 10; + x /= 10; + } + return sum; + } +} diff --git a/src/main/java/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/readme.md b/src/main/java/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/readme.md new file mode 100644 index 000000000..559b822eb --- /dev/null +++ b/src/main/java/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/java/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/Solution.java b/src/main/java/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/Solution.java new file mode 100644 index 000000000..dc2d1e90d --- /dev/null +++ b/src/main/java/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/Solution.java @@ -0,0 +1,27 @@ +package g3301_3400.s3301_maximize_the_total_height_of_unique_towers; + +// #Medium #Array #Sorting #Greedy #2024_10_01_Time_49_ms_(92.39%)_Space_57.9_MB_(70.01%) + +import java.util.Arrays; + +public class Solution { + public long maximumTotalSum(int[] maximumHeight) { + Arrays.sort(maximumHeight); + long result = maximumHeight[maximumHeight.length - 1]; + long previousHeight = maximumHeight[maximumHeight.length - 1]; + for (int i = maximumHeight.length - 2; i >= 0; i--) { + if (previousHeight == 1) { + return -1; + } + long height = maximumHeight[i]; + if (height >= previousHeight) { + result = result + previousHeight - 1; + previousHeight = previousHeight - 1; + } else { + result = result + height; + previousHeight = height; + } + } + return result; + } +} diff --git a/src/main/java/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/readme.md b/src/main/java/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/readme.md new file mode 100644 index 000000000..debc12a57 --- /dev/null +++ b/src/main/java/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/java/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/Solution.java b/src/main/java/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/Solution.java new file mode 100644 index 000000000..fca7f5b0f --- /dev/null +++ b/src/main/java/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/Solution.java @@ -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_21_ms_(97.32%)_Space_74.3_MB_(74.55%) + +public class Solution { + public int[] validSequence(String word1, String word2) { + char[] c1 = word1.toCharArray(); + char[] c2 = word2.toCharArray(); + int[] dp = new int[c1.length + 1]; + int j = c2.length - 1; + for (int i = c1.length - 1; i >= 0; i--) { + if (j >= 0 && c1[i] == c2[j]) { + dp[i] = dp[i + 1] + 1; + j--; + } else { + dp[i] = dp[i + 1]; + } + } + int[] ans = new int[c2.length]; + int i = 0; + j = 0; + while (i < c1.length && j < c2.length) { + if (c1[i] == c2[j]) { + ans[j] = i; + j++; + } else { + if (dp[i + 1] >= c2.length - 1 - j) { + ans[j] = i; + j++; + i++; + break; + } + } + i++; + } + if (j < c2.length && i == c1.length) { + return new int[0]; + } + while (j < c2.length && i < c1.length) { + if (c2[j] == c1[i]) { + ans[j] = i; + j++; + } + i++; + } + return ans; + } +} diff --git a/src/main/java/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/readme.md b/src/main/java/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/readme.md new file mode 100644 index 000000000..feaa7b957 --- /dev/null +++ b/src/main/java/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/java/g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/Solution.java b/src/main/java/g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/Solution.java new file mode 100644 index 000000000..06c638c02 --- /dev/null +++ b/src/main/java/g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/Solution.java @@ -0,0 +1,58 @@ +package g3301_3400.s3303_find_the_occurrence_of_first_almost_equal_substring; + +// #Hard #String #String_Matching #2024_10_01_Time_39_ms_(100.00%)_Space_46.1_MB_(100.00%) + +public class Solution { + public int minStartingIndex(String s, String pattern) { + int n = s.length(); + int left = 0; + int right = 0; + int[] f1 = new int[26]; + int[] f2 = new int[26]; + for (char ch : pattern.toCharArray()) { + f2[ch - 'a']++; + } + while (right < n) { + char ch = s.charAt(right); + f1[ch - 'a']++; + if (right - left + 1 == pattern.length() + 1) { + f1[s.charAt(left) - 'a']--; + left += 1; + } + if (right - left + 1 == pattern.length() && check(f1, f2, left, s, pattern)) { + return left; + } + right += 1; + } + return -1; + } + + private boolean check(int[] f1, int[] f2, int left, String s, String pattern) { + int cnt = 0; + for (int i = 0; i < 26; i++) { + if (f1[i] != f2[i]) { + if ((Math.abs(f1[i] - f2[i]) > 1) || (Math.abs(f1[i] - f2[i]) != 1 && cnt == 2)) { + return false; + } + cnt += 1; + } + } + cnt = 0; + int start = 0; + int end = pattern.length() - 1; + while (start <= end) { + if (s.charAt(start + left) != pattern.charAt(start)) { + cnt += 1; + } + if (start + left != left + end && s.charAt(left + end) != pattern.charAt(end)) { + cnt += 1; + } + if (cnt >= 2) { + return false; + } + start++; + end--; + } + return true; + } +} diff --git a/src/main/java/g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/readme.md b/src/main/java/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/java/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/java/g3301_3400/s3304_find_the_k_th_character_in_string_game_i/Solution.java b/src/main/java/g3301_3400/s3304_find_the_k_th_character_in_string_game_i/Solution.java new file mode 100644 index 000000000..5101ac4f8 --- /dev/null +++ b/src/main/java/g3301_3400/s3304_find_the_k_th_character_in_string_game_i/Solution.java @@ -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_0_ms_(100.00%)_Space_41.2_MB_(99.17%) + +public class Solution { + public char kthCharacter(int k) { + // Initialize the length of the current string + // Initial length when word = "a" + int length = 1; + + // Find the total length after enough iterations + while (length < k) { + length *= 2; + } + // Trace back to the original character + // Start with 'a' + char 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/java/g3301_3400/s3304_find_the_k_th_character_in_string_game_i/readme.md b/src/main/java/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/java/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/java/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i/Solution.java b/src/main/java/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i/Solution.java new file mode 100644 index 000000000..497e6b6c5 --- /dev/null +++ b/src/main/java/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i/Solution.java @@ -0,0 +1,57 @@ +package g3301_3400.s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i; + +// #Medium #String #Hash_Table #Sliding_Window #2024_10_01_Time_2_ms_(99.72%)_Space_42.2_MB_(98.48%) + +public class Solution { + public int countOfSubstrings(String word, int k) { + char[] arr = word.toCharArray(); + int[] map = new int[26]; + map[0]++; + map['e' - 'a']++; + map['i' - 'a']++; + map['o' - 'a']++; + map['u' - 'a']++; + int need = 5; + int ans = 0; + int consCnt = 0; + int j = 0; + for (int i = 0; i < arr.length; i++) { + while (j < arr.length && (need > 0 || consCnt < k)) { + if (isVowel(arr[j])) { + map[arr[j] - 'a']--; + if (map[arr[j] - 'a'] == 0) { + need--; + } + } else { + consCnt++; + } + j++; + } + if (need == 0 && consCnt == k) { + ans++; + int m = j; + while (m < arr.length) { + if (isVowel(arr[m])) { + ans++; + } else { + break; + } + m++; + } + } + if (isVowel(arr[i])) { + map[arr[i] - 'a']++; + if (map[arr[i] - 'a'] == 1) { + need++; + } + } else { + consCnt--; + } + } + return ans; + } + + private boolean isVowel(char ch) { + return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'; + } +} diff --git a/src/main/java/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i/readme.md b/src/main/java/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/java/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/java/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/Solution.java b/src/main/java/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/Solution.java new file mode 100644 index 000000000..038464ff5 --- /dev/null +++ b/src/main/java/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/Solution.java @@ -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_340_ms_(44.09%)_Space_46.3_MB_(62.47%) + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class Solution { + public long countOfSubstrings(String word, int k) { + return countOfSubstringHavingAtleastXConsonants(word, k) + - countOfSubstringHavingAtleastXConsonants(word, k + 1); + } + + private long countOfSubstringHavingAtleastXConsonants(String word, int k) { + int start = 0; + int end = 0; + Set vowels = new HashSet<>(); + vowels.add('a'); + vowels.add('e'); + vowels.add('i'); + vowels.add('o'); + vowels.add('u'); + int consonants = 0; + Map map = new HashMap<>(); + long res = 0; + while (end < word.length()) { + char ch = word.charAt(end); + // adding vowel or consonants; + if (vowels.contains(ch)) { + if (map.containsKey(ch)) { + map.put(ch, map.get(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; + char ch1 = word.charAt(start); + if (vowels.contains(ch1)) { + int temp = map.get(ch1) - 1; + if (temp == 0) { + map.remove(ch1); + } else { + map.put(ch1, temp); + } + } else { + consonants--; + } + start++; + } + end++; + } + return res; + } +} diff --git a/src/main/java/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/readme.md b/src/main/java/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/java/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/java/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii/Solution.java b/src/main/java/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii/Solution.java new file mode 100644 index 000000000..07a08c36f --- /dev/null +++ b/src/main/java/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii/Solution.java @@ -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_1_ms_(99.65%)_Space_43.2_MB_(59.72%) + +public class Solution { + public char kthCharacter(long k, int[] operations) { + if (k == 1) { + return 'a'; + } + long len = 1; + long newK = -1; + int operation = -1; + for (int ope : operations) { + len *= 2; + if (len >= k) { + operation = ope; + newK = k - len / 2; + break; + } + } + char ch = kthCharacter(newK, operations); + if (operation == 0) { + return ch; + } + return ch == 'z' ? 'a' : (char) (ch + 1); + } +} diff --git a/src/main/java/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii/readme.md b/src/main/java/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/java/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/java/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/SolutionTest.java b/src/test/java/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/SolutionTest.java new file mode 100644 index 000000000..bed399cc4 --- /dev/null +++ b/src/test/java/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/SolutionTest.java @@ -0,0 +1,23 @@ +package g3201_3300.s3300_minimum_element_after_replacement_with_digit_sum; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minElement() { + assertThat(new Solution().minElement(new int[] {10, 12, 13, 14}), equalTo(1)); + } + + @Test + void minElement2() { + assertThat(new Solution().minElement(new int[] {1, 2, 3, 4}), equalTo(1)); + } + + @Test + void minElement3() { + assertThat(new Solution().minElement(new int[] {999, 19, 199}), equalTo(10)); + } +} diff --git a/src/test/java/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/SolutionTest.java b/src/test/java/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/SolutionTest.java new file mode 100644 index 000000000..30b57b653 --- /dev/null +++ b/src/test/java/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/SolutionTest.java @@ -0,0 +1,23 @@ +package g3301_3400.s3301_maximize_the_total_height_of_unique_towers; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maximumTotalSum() { + assertThat(new Solution().maximumTotalSum(new int[] {2, 3, 4, 3}), equalTo(10L)); + } + + @Test + void maximumTotalSum2() { + assertThat(new Solution().maximumTotalSum(new int[] {15, 10}), equalTo(25L)); + } + + @Test + void maximumTotalSum3() { + assertThat(new Solution().maximumTotalSum(new int[] {2, 2, 1}), equalTo(-1L)); + } +} diff --git a/src/test/java/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/SolutionTest.java b/src/test/java/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/SolutionTest.java new file mode 100644 index 000000000..8ac10ec35 --- /dev/null +++ b/src/test/java/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/SolutionTest.java @@ -0,0 +1,28 @@ +package g3301_3400.s3302_find_the_lexicographically_smallest_valid_sequence; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void validSequence() { + assertThat(new Solution().validSequence("vbcca", "abc"), equalTo(new int[] {0, 1, 2})); + } + + @Test + void validSequence2() { + assertThat(new Solution().validSequence("bacdc", "abc"), equalTo(new int[] {1, 2, 4})); + } + + @Test + void validSequence3() { + assertThat(new Solution().validSequence("aaaaaa", "aaabc"), equalTo(new int[] {})); + } + + @Test + void validSequence4() { + assertThat(new Solution().validSequence("abc", "ab"), equalTo(new int[] {0, 1})); + } +} diff --git a/src/test/java/g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/SolutionTest.java b/src/test/java/g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/SolutionTest.java new file mode 100644 index 000000000..378e1815d --- /dev/null +++ b/src/test/java/g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/SolutionTest.java @@ -0,0 +1,28 @@ +package g3301_3400.s3303_find_the_occurrence_of_first_almost_equal_substring; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minStartingIndex() { + assertThat(new Solution().minStartingIndex("abcdefg", "bcdffg"), equalTo(1)); + } + + @Test + void minStartingIndex2() { + assertThat(new Solution().minStartingIndex("ababbababa", "bacaba"), equalTo(4)); + } + + @Test + void minStartingIndex3() { + assertThat(new Solution().minStartingIndex("abcd", "dba"), equalTo(-1)); + } + + @Test + void minStartingIndex4() { + assertThat(new Solution().minStartingIndex("dde", "d"), equalTo(0)); + } +} diff --git a/src/test/java/g3301_3400/s3304_find_the_k_th_character_in_string_game_i/SolutionTest.java b/src/test/java/g3301_3400/s3304_find_the_k_th_character_in_string_game_i/SolutionTest.java new file mode 100644 index 000000000..fcd45c721 --- /dev/null +++ b/src/test/java/g3301_3400/s3304_find_the_k_th_character_in_string_game_i/SolutionTest.java @@ -0,0 +1,18 @@ +package g3301_3400.s3304_find_the_k_th_character_in_string_game_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void kthCharacter() { + assertThat(new Solution().kthCharacter(5), equalTo('b')); + } + + @Test + void kthCharacter2() { + assertThat(new Solution().kthCharacter(10), equalTo('c')); + } +} diff --git a/src/test/java/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i/SolutionTest.java b/src/test/java/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i/SolutionTest.java new file mode 100644 index 000000000..823eaaf54 --- /dev/null +++ b/src/test/java/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i/SolutionTest.java @@ -0,0 +1,23 @@ +package g3301_3400.s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countOfSubstrings() { + assertThat(new Solution().countOfSubstrings("aeioqq", 1), equalTo(0)); + } + + @Test + void countOfSubstrings2() { + assertThat(new Solution().countOfSubstrings("aeiou", 0), equalTo(1)); + } + + @Test + void countOfSubstrings3() { + assertThat(new Solution().countOfSubstrings("ieaouqqieaouqq", 1), equalTo(3)); + } +} diff --git a/src/test/java/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/SolutionTest.java b/src/test/java/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/SolutionTest.java new file mode 100644 index 000000000..e60b13738 --- /dev/null +++ b/src/test/java/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/SolutionTest.java @@ -0,0 +1,23 @@ +package g3301_3400.s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countOfSubstrings() { + assertThat(new Solution().countOfSubstrings("aeioqq", 1), equalTo(0L)); + } + + @Test + void countOfSubstrings2() { + assertThat(new Solution().countOfSubstrings("aeiou", 0), equalTo(1L)); + } + + @Test + void countOfSubstrings3() { + assertThat(new Solution().countOfSubstrings("ieaouqqieaouqq", 1), equalTo(3L)); + } +} diff --git a/src/test/java/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii/SolutionTest.java b/src/test/java/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii/SolutionTest.java new file mode 100644 index 000000000..f0df1a836 --- /dev/null +++ b/src/test/java/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii/SolutionTest.java @@ -0,0 +1,18 @@ +package g3301_3400.s3307_find_the_k_th_character_in_string_game_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void kthCharacter() { + assertThat(new Solution().kthCharacter(5, new int[] {0, 0, 0}), equalTo('a')); + } + + @Test + void kthCharacter2() { + assertThat(new Solution().kthCharacter(10, new int[] {0, 1, 0, 1}), equalTo('b')); + } +}