From 34adc7f41a460cbfdfcfda2448f9073a7025248c Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 30 Sep 2024 09:11:59 +0300 Subject: [PATCH 1/6] Added tasks 3300-3307 --- .../Solution.java | 31 +++++++++ .../readme.md | 44 +++++++++++++ .../Solution.java | 27 ++++++++ .../readme.md | 47 ++++++++++++++ .../Solution.java | 39 +++++++++++ .../readme.md | 65 +++++++++++++++++++ .../Solution.java | 49 ++++++++++++++ .../readme.md | 50 ++++++++++++++ .../Solution.java | 27 ++++++++ .../readme.md | 41 ++++++++++++ .../Solution.java | 38 +++++++++++ .../readme.md | 47 ++++++++++++++ .../Solution.java | 60 +++++++++++++++++ .../readme.md | 47 ++++++++++++++ .../Solution.java | 28 ++++++++ .../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, 876 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..e6f894004 --- /dev/null +++ b/src/main/java/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/Solution.java @@ -0,0 +1,31 @@ +package g3201_3300.s3300_minimum_element_after_replacement_with_digit_sum; + +// #Easy #2024_09_30_Time_4_ms_(100.00%)_Space_43.3_MB_(100.00%) + +import java.util.Arrays; + +public class Solution { + public int minElement(int[] nums) { + int n = nums.length; + int[] arr = new int[n]; + + for (int i = 0; i < n; i++) { + int sum = sumOfDigits(nums[i]); + arr[i] = sum; + } + Arrays.sort(arr); + return arr[0]; + } + + private int sumOfDigits(int num) { + int sum = 0; + if (num <= 9) { + return num; + } + while (num > 0) { + sum += num % 10; + num /= 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..d7d1b902b --- /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 #2024_09_30_Time_49_ms_(100.00%)_Space_57.8_MB_(100.00%) + +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..4f37ecbc2 --- /dev/null +++ b/src/main/java/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/Solution.java @@ -0,0 +1,39 @@ +package g3301_3400.s3302_find_the_lexicographically_smallest_valid_sequence; + +// #Medium #2024_09_30_Time_33_ms_(100.00%)_Space_79.5_MB_(50.00%) + +import java.util.Arrays; + +public class Solution { + public int[] validSequence(String word1, String word2) { + int n = word1.length(); + int m = word2.length(); + int[] revGreedyMatchInd = new int[m]; + Arrays.fill(revGreedyMatchInd, -1); + { + int i = n - 1; + int j = m - 1; + while (j >= 0 && i >= 0) { + if (word1.charAt(i) == word2.charAt(j)) { + revGreedyMatchInd[j--] = i; + } + i--; + } + } + boolean canSkip = true; + int j = 0; + int i = 0; + while (i < n && j < m && m - j <= n - i) { + if (word1.charAt(i) == word2.charAt(j)) { + revGreedyMatchInd[j++] = i; + } else if (canSkip && (j == m - 1 || i < revGreedyMatchInd[j + 1])) { + revGreedyMatchInd[j++] = i; + canSkip = false; + } else if (!canSkip && revGreedyMatchInd[j] == -1) { + break; + } + i++; + } + return j == m ? revGreedyMatchInd : new int[0]; + } +} 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..80e8938bf --- /dev/null +++ b/src/main/java/g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/Solution.java @@ -0,0 +1,49 @@ +package g3301_3400.s3303_find_the_occurrence_of_first_almost_equal_substring; + +// #Hard #2024_09_30_Time_1198_ms_(100.00%)_Space_97.4_MB_(100.00%) + +import java.util.HashMap; +import java.util.Map; + +public class Solution { + public int minStartingIndex(String s, String p) { + Map mp = new HashMap<>(); + long hash = 0; + long base = 26; + long d = 1; + long hashP = 0; + long mod = 10000000000283L; + int n = s.length(); + int sz = p.length(); + // Rolling hash for string s + for (int i = 0; i < n; i++) { + hash = (hash * base + s.charAt(i)) % mod; + if (i >= sz) { + hash = (mod + hash - d * s.charAt(i - sz) % mod) % mod; + } else { + d = d * base % mod; + } + if (i >= sz - 1 && !mp.containsKey(hash)) { + mp.put(hash, i - sz + 1); + } + } + // Hash for the pattern p + for (int i = 0; i < sz; i++) { + hashP = (hashP * base + p.charAt(i)) % mod; + } + d = 1; + int ans = Integer.MAX_VALUE; + // Find the minimum index with almost equal string + for (int i = sz - 1; i >= 0; i--) { + long newhashP = (mod + hashP - d * p.charAt(i) % mod) % mod; + for (char a = 'a'; a <= 'z'; a++) { + long candidateHash = (newhashP + d * a % mod) % mod; + if (mp.containsKey(candidateHash)) { + ans = Math.min(ans, mp.get(candidateHash)); + } + } + d = d * base % mod; + } + return ans == Integer.MAX_VALUE ? -1 : ans; + } +} 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..1d142d637 --- /dev/null +++ b/src/main/java/g3301_3400/s3304_find_the_k_th_character_in_string_game_i/Solution.java @@ -0,0 +1,27 @@ +package g3301_3400.s3304_find_the_k_th_character_in_string_game_i; + +// #Easy #2024_09_30_Time_16_ms_(100.00%)_Space_44.5_MB_(100.00%) + +public class Solution { + public char kthCharacter(int k) { + StringBuilder sb = new StringBuilder(); + char c = 'a'; + sb.append(c); + String s = sb.toString(); + while (s.length() <= k) { + s = sb.toString(); + char[] cq = s.toCharArray(); + for (char c1 : cq) { + int ascii = (int) c1; + c1 = (char) (ascii + 1); + sb.append(c1); + } + } + for (int i = 0; i < sb.toString().length(); i++) { + if (i == k) { + return sb.toString().charAt(i - 1); + } + } + return '\0'; + } +} 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..dc24b6d18 --- /dev/null +++ b/src/main/java/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i/Solution.java @@ -0,0 +1,38 @@ +package g3301_3400.s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i; + +// #Medium #2024_09_30_Time_1998_ms_(100.00%)_Space_45.5_MB_(50.00%) + +import java.util.HashSet; + +public class Solution { + public int countOfSubstrings(String word, int k) { + int possibleSubstring = 0; + HashSet vowelContainer = new HashSet<>(); + String vowel = "aeiou"; + for (char ch : vowel.toCharArray()) { + vowelContainer.add(ch); + } + for (int i = 0; i < word.length(); i++) { + for (int j = i; j < word.length(); j++) { + if (checkValid(word, i, j, vowelContainer, k)) { + possibleSubstring++; + } + } + } + return possibleSubstring; + } + + private boolean checkValid( + String word, int start, int end, HashSet vowelContainer, int limit) { + HashSet duplicateCheck = new HashSet<>(); + int consonants = 0; + for (int i = start; i <= end; i++) { + if (!vowelContainer.contains(word.charAt(i))) { + consonants++; + } else { + duplicateCheck.add(word.charAt(i)); + } + } + return duplicateCheck.size() == 5 && consonants == limit; + } +} 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..246c899a8 --- /dev/null +++ b/src/main/java/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/Solution.java @@ -0,0 +1,60 @@ +package g3301_3400.s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii; + +// #Medium #2024_09_30_Time_341_ms_(100.00%)_Space_46.1_MB_(100.00%) + +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..5c5d3ac15 --- /dev/null +++ b/src/main/java/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii/Solution.java @@ -0,0 +1,28 @@ +package g3301_3400.s3307_find_the_k_th_character_in_string_game_ii; + +// #Hard #2024_09_30_Time_1_ms_(100.00%)_Space_43.4_MB_(100.00%) + +public class Solution { + public char kthCharacter(long k, int[] operations) { + if (k == 1) { + return 'a'; + } + int n = operations.length; + 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')); + } +} From 05cc3f1175d76a424b365f45f4b71cb6ba476c14 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Mon, 30 Sep 2024 09:20:02 +0300 Subject: [PATCH 2/6] Fixed sonar --- .../Solution.java | 34 +++++++++---------- .../Solution.java | 2 +- .../Solution.java | 1 - 3 files changed, 17 insertions(+), 20 deletions(-) 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 index 4f37ecbc2..07ec077f5 100644 --- 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 @@ -10,30 +10,28 @@ public int[] validSequence(String word1, String word2) { int m = word2.length(); int[] revGreedyMatchInd = new int[m]; Arrays.fill(revGreedyMatchInd, -1); - { - int i = n - 1; - int j = m - 1; - while (j >= 0 && i >= 0) { - if (word1.charAt(i) == word2.charAt(j)) { - revGreedyMatchInd[j--] = i; - } - i--; + int i = n - 1; + int j = m - 1; + while (j >= 0 && i >= 0) { + if (word1.charAt(i) == word2.charAt(j)) { + revGreedyMatchInd[j--] = i; } + i--; } boolean canSkip = true; - int j = 0; - int i = 0; - while (i < n && j < m && m - j <= n - i) { - if (word1.charAt(i) == word2.charAt(j)) { - revGreedyMatchInd[j++] = i; - } else if (canSkip && (j == m - 1 || i < revGreedyMatchInd[j + 1])) { - revGreedyMatchInd[j++] = i; + int j1 = 0; + int i1 = 0; + while (i1 < n && j1 < m && m - j1 <= n - i1) { + if (word1.charAt(i1) == word2.charAt(j1)) { + revGreedyMatchInd[j1++] = i1; + } else if (canSkip && (j1 == m - 1 || i1 < revGreedyMatchInd[j1 + 1])) { + revGreedyMatchInd[j1++] = i1; canSkip = false; - } else if (!canSkip && revGreedyMatchInd[j] == -1) { + } else if (!canSkip && revGreedyMatchInd[j1] == -1) { break; } - i++; + i1++; } - return j == m ? revGreedyMatchInd : new int[0]; + return j1 == m ? revGreedyMatchInd : new int[0]; } } 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 index 1d142d637..460cbb1e6 100644 --- 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 @@ -12,7 +12,7 @@ public char kthCharacter(int k) { s = sb.toString(); char[] cq = s.toCharArray(); for (char c1 : cq) { - int ascii = (int) c1; + int ascii = c1; c1 = (char) (ascii + 1); sb.append(c1); } 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 index 5c5d3ac15..9eb086490 100644 --- 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 @@ -7,7 +7,6 @@ public char kthCharacter(long k, int[] operations) { if (k == 1) { return 'a'; } - int n = operations.length; long len = 1; long newK = -1; int operation = -1; From 946abf85f81ae6e84ae6f12ddbc058ec943ba333 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 1 Oct 2024 06:15:03 +0300 Subject: [PATCH 3/6] Improved tags --- .../Solution.java | 29 +++---- .../Solution.java | 2 +- .../Solution.java | 64 ++++++++------ .../Solution.java | 83 +++++++++++-------- .../Solution.java | 41 +++++---- .../Solution.java | 72 ++++++++++------ .../Solution.java | 3 +- .../Solution.java | 2 +- 8 files changed, 170 insertions(+), 126 deletions(-) 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 index e6f894004..0c5a4c795 100644 --- 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 @@ -1,30 +1,21 @@ package g3201_3300.s3300_minimum_element_after_replacement_with_digit_sum; -// #Easy #2024_09_30_Time_4_ms_(100.00%)_Space_43.3_MB_(100.00%) +// #Easy #Array #Math #2024_10_01_Time_1_ms_(100.00%)_Space_42.9_MB_(75.97%) -import java.util.Arrays; - -public class Solution { +class Solution { public int minElement(int[] nums) { - int n = nums.length; - int[] arr = new int[n]; - - for (int i = 0; i < n; i++) { - int sum = sumOfDigits(nums[i]); - arr[i] = sum; + int min = Integer.MAX_VALUE; + for (int x : nums) { + min = Math.min(min, solve(x)); } - Arrays.sort(arr); - return arr[0]; + return min; } - private int sumOfDigits(int num) { + private int solve(int x) { int sum = 0; - if (num <= 9) { - return num; - } - while (num > 0) { - sum += num % 10; - num /= 10; + while (x != 0) { + sum += x % 10; + x /= 10; } return sum; } 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 index d7d1b902b..dc2d1e90d 100644 --- 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 @@ -1,6 +1,6 @@ package g3301_3400.s3301_maximize_the_total_height_of_unique_towers; -// #Medium #2024_09_30_Time_49_ms_(100.00%)_Space_57.8_MB_(100.00%) +// #Medium #Array #Sorting #Greedy #2024_10_01_Time_49_ms_(92.39%)_Space_57.9_MB_(70.01%) import java.util.Arrays; 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 index 07ec077f5..fca7f5b0f 100644 --- 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 @@ -1,37 +1,49 @@ package g3301_3400.s3302_find_the_lexicographically_smallest_valid_sequence; -// #Medium #2024_09_30_Time_33_ms_(100.00%)_Space_79.5_MB_(50.00%) - -import java.util.Arrays; +// #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) { - int n = word1.length(); - int m = word2.length(); - int[] revGreedyMatchInd = new int[m]; - Arrays.fill(revGreedyMatchInd, -1); - int i = n - 1; - int j = m - 1; - while (j >= 0 && i >= 0) { - if (word1.charAt(i) == word2.charAt(j)) { - revGreedyMatchInd[j--] = i; + 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--; + i++; + } + if (j < c2.length && i == c1.length) { + return new int[0]; } - boolean canSkip = true; - int j1 = 0; - int i1 = 0; - while (i1 < n && j1 < m && m - j1 <= n - i1) { - if (word1.charAt(i1) == word2.charAt(j1)) { - revGreedyMatchInd[j1++] = i1; - } else if (canSkip && (j1 == m - 1 || i1 < revGreedyMatchInd[j1 + 1])) { - revGreedyMatchInd[j1++] = i1; - canSkip = false; - } else if (!canSkip && revGreedyMatchInd[j1] == -1) { - break; + while (j < c2.length && i < c1.length) { + if (c2[j] == c1[i]) { + ans[j] = i; + j++; } - i1++; + i++; } - return j1 == m ? revGreedyMatchInd : new int[0]; + return ans; } } 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 index 80e8938bf..ebae7870c 100644 --- 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 @@ -1,49 +1,60 @@ package g3301_3400.s3303_find_the_occurrence_of_first_almost_equal_substring; -// #Hard #2024_09_30_Time_1198_ms_(100.00%)_Space_97.4_MB_(100.00%) - -import java.util.HashMap; -import java.util.Map; +// #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 p) { - Map mp = new HashMap<>(); - long hash = 0; - long base = 26; - long d = 1; - long hashP = 0; - long mod = 10000000000283L; + public int minStartingIndex(String s, String pattern) { int n = s.length(); - int sz = p.length(); - // Rolling hash for string s - for (int i = 0; i < n; i++) { - hash = (hash * base + s.charAt(i)) % mod; - if (i >= sz) { - hash = (mod + hash - d * s.charAt(i - sz) % mod) % mod; - } else { - d = d * base % mod; + 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 (i >= sz - 1 && !mp.containsKey(hash)) { - mp.put(hash, i - sz + 1); + if (right - left + 1 == pattern.length()) { + if (check(f1, f2, left, right, s, pattern) == true) { + return left; + } } + right += 1; } - // Hash for the pattern p - for (int i = 0; i < sz; i++) { - hashP = (hashP * base + p.charAt(i)) % mod; - } - d = 1; - int ans = Integer.MAX_VALUE; - // Find the minimum index with almost equal string - for (int i = sz - 1; i >= 0; i--) { - long newhashP = (mod + hashP - d * p.charAt(i) % mod) % mod; - for (char a = 'a'; a <= 'z'; a++) { - long candidateHash = (newhashP + d * a % mod) % mod; - if (mp.containsKey(candidateHash)) { - ans = Math.min(ans, mp.get(candidateHash)); + return -1; + } + + private boolean check(int[] f1, int[] f2, int left, int right, 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; } - d = d * base % mod; + start++; + end--; } - return ans == Integer.MAX_VALUE ? -1 : ans; + return true; } } 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 index 460cbb1e6..5101ac4f8 100644 --- 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 @@ -1,27 +1,34 @@ package g3301_3400.s3304_find_the_k_th_character_in_string_game_i; -// #Easy #2024_09_30_Time_16_ms_(100.00%)_Space_44.5_MB_(100.00%) +// #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) { - StringBuilder sb = new StringBuilder(); - char c = 'a'; - sb.append(c); - String s = sb.toString(); - while (s.length() <= k) { - s = sb.toString(); - char[] cq = s.toCharArray(); - for (char c1 : cq) { - int ascii = c1; - c1 = (char) (ascii + 1); - sb.append(c1); - } + // 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; } - for (int i = 0; i < sb.toString().length(); i++) { - if (i == k) { - return sb.toString().charAt(i - 1); + // 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 '\0'; + return currentChar; } } 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 index dc24b6d18..d9e326a54 100644 --- 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 @@ -1,38 +1,60 @@ package g3301_3400.s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i; -// #Medium #2024_09_30_Time_1998_ms_(100.00%)_Space_45.5_MB_(50.00%) - -import java.util.HashSet; +// #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) { - int possibleSubstring = 0; - HashSet vowelContainer = new HashSet<>(); - String vowel = "aeiou"; - for (char ch : vowel.toCharArray()) { - vowelContainer.add(ch); - } - for (int i = 0; i < word.length(); i++) { - for (int j = i; j < word.length(); j++) { - if (checkValid(word, i, j, vowelContainer, k)) { - possibleSubstring++; + char[] arr = word.toCharArray(); + int[] map = new int[26]; + map['a' - 'a']++; + 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 possibleSubstring; + return ans; } - private boolean checkValid( - String word, int start, int end, HashSet vowelContainer, int limit) { - HashSet duplicateCheck = new HashSet<>(); - int consonants = 0; - for (int i = start; i <= end; i++) { - if (!vowelContainer.contains(word.charAt(i))) { - consonants++; - } else { - duplicateCheck.add(word.charAt(i)); - } + private boolean isVowel(char ch) { + if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { + return true; } - return duplicateCheck.size() == 5 && consonants == limit; + return false; } } 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 index 246c899a8..038464ff5 100644 --- 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 @@ -1,6 +1,7 @@ package g3301_3400.s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii; -// #Medium #2024_09_30_Time_341_ms_(100.00%)_Space_46.1_MB_(100.00%) +// #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; 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 index 9eb086490..07a08c36f 100644 --- 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 @@ -1,6 +1,6 @@ package g3301_3400.s3307_find_the_k_th_character_in_string_game_ii; -// #Hard #2024_09_30_Time_1_ms_(100.00%)_Space_43.4_MB_(100.00%) +// #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) { From 7699ae3aff5bf3de760f716540204f6e37166faa Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 1 Oct 2024 06:16:39 +0300 Subject: [PATCH 4/6] Improved 3300 --- .../Solution.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 0c5a4c795..f66bd4d68 100644 --- 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 @@ -2,7 +2,7 @@ // #Easy #Array #Math #2024_10_01_Time_1_ms_(100.00%)_Space_42.9_MB_(75.97%) -class Solution { +public class Solution { public int minElement(int[] nums) { int min = Integer.MAX_VALUE; for (int x : nums) { From 2965cd2ba29a69cdf0851a5a42787ccabf2e87f4 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 1 Oct 2024 06:35:37 +0300 Subject: [PATCH 5/6] Fixed sonar --- .../Solution.java | 8 +++----- .../Solution.java | 5 +---- 2 files changed, 4 insertions(+), 9 deletions(-) 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 index ebae7870c..06c638c02 100644 --- 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 @@ -19,17 +19,15 @@ public int minStartingIndex(String s, String pattern) { f1[s.charAt(left) - 'a']--; left += 1; } - if (right - left + 1 == pattern.length()) { - if (check(f1, f2, left, right, s, pattern) == true) { - return left; - } + 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, int right, String s, String pattern) { + 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]) { 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 index d9e326a54..bcbce9edd 100644 --- 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 @@ -52,9 +52,6 @@ public int countOfSubstrings(String word, int k) { } private boolean isVowel(char ch) { - if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { - return true; - } - return false; + return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'; } } From 5128dbcf7036529df25b0e28158fc32e949dad5d Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 1 Oct 2024 06:36:42 +0300 Subject: [PATCH 6/6] Fixed sonar --- .../Solution.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index bcbce9edd..497e6b6c5 100644 --- 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 @@ -6,7 +6,7 @@ public class Solution { public int countOfSubstrings(String word, int k) { char[] arr = word.toCharArray(); int[] map = new int[26]; - map['a' - 'a']++; + map[0]++; map['e' - 'a']++; map['i' - 'a']++; map['o' - 'a']++;