forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
518 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/main/java/g3001_3100/s3031_minimum_time_to_revert_word_to_initial_state_ii/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package g3001_3100.s3031_minimum_time_to_revert_word_to_initial_state_ii; | ||
|
||
// #Hard #String #Hash_Function #String_Matching #Rolling_Hash | ||
// #2024_03_01_Time_24_ms_(74.98%)_Space_55.1_MB_(14.85%) | ||
|
||
public class Solution { | ||
public int minimumTimeToInitialState(String w, int q) { | ||
char[] c = w.toCharArray(); | ||
int[] lps = new int[c.length]; | ||
int k; | ||
for (int i = 1; i < lps.length; i++) { | ||
if (c[i] == c[0]) { | ||
lps[i] = 1; | ||
} | ||
k = lps[i - 1]; | ||
while (k > 0) { | ||
if (c[k] == c[i]) { | ||
lps[i] = k + 1; | ||
break; | ||
} | ||
k = lps[k - 1]; | ||
} | ||
} | ||
k = lps[lps.length - 1]; | ||
while (k > 0) { | ||
if ((c.length - k) % q == 0) { | ||
return (c.length - k) / q; | ||
} | ||
k = lps[k - 1]; | ||
} | ||
return (c.length + q - 1) / q; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...java/g3001_3100/s3031_minimum_time_to_revert_word_to_initial_state_ii/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
3031\. Minimum Time to Revert Word to Initial State II | ||
|
||
Hard | ||
|
||
You are given a **0-indexed** string `word` and an integer `k`. | ||
|
||
At every second, you must perform the following operations: | ||
|
||
* Remove the first `k` characters of `word`. | ||
* Add any `k` characters to the end of `word`. | ||
|
||
**Note** that you do not necessarily need to add the same characters that you removed. However, you must perform **both** operations at every second. | ||
|
||
Return _the **minimum** time greater than zero required for_ `word` _to revert to its **initial** state_. | ||
|
||
**Example 1:** | ||
|
||
**Input:** word = "abacaba", k = 3 | ||
|
||
**Output:** 2 | ||
|
||
**Explanation:** At the 1st second, we remove characters "aba" from the prefix of word, and add characters "bac" to the end of word. Thus, word becomes equal to "cababac". At the 2nd second, we remove characters "cab" from the prefix of word, and add "aba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state. | ||
|
||
It can be shown that 2 seconds is the minimum time greater than zero required for word to revert to its initial state. | ||
|
||
**Example 2:** | ||
|
||
**Input:** word = "abacaba", k = 4 | ||
|
||
**Output:** 1 | ||
|
||
**Explanation:** At the 1st second, we remove characters "abac" from the prefix of word, and add characters "caba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state. | ||
|
||
It can be shown that 1 second is the minimum time greater than zero required for word to revert to its initial state. | ||
|
||
**Example 3:** | ||
|
||
**Input:** word = "abcbabcd", k = 2 | ||
|
||
**Output:** 4 | ||
|
||
**Explanation:** At every second, we will remove the first 2 characters of word, and add the same characters to the end of word. After 4 seconds, word becomes equal to "abcbabcd" and reverts to its initial state. | ||
|
||
It can be shown that 4 seconds is the minimum time greater than zero required for word to revert to its initial state. | ||
|
||
**Constraints:** | ||
|
||
* <code>1 <= word.length <= 10<sup>6</sup></code> | ||
* `1 <= k <= word.length` | ||
* `word` consists only of lowercase English letters. |
22 changes: 22 additions & 0 deletions
22
src/main/java/g3001_3100/s3033_modify_the_matrix/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package g3001_3100.s3033_modify_the_matrix; | ||
|
||
// #Easy #Array #Matrix #2024_03_01_Time_1_ms_(100.00%)_Space_45.4_MB_(77.37%) | ||
|
||
public class Solution { | ||
public int[][] modifiedMatrix(int[][] matrix) { | ||
for (int i = 0; i < matrix.length; i++) { | ||
for (int j = 0; j < matrix[0].length; j++) { | ||
if (matrix[i][j] == -1) { | ||
int y = 0; | ||
for (int[] ints : matrix) { | ||
if (ints[j] > y) { | ||
y = ints[j]; | ||
} | ||
} | ||
matrix[i][j] = y; | ||
} | ||
} | ||
} | ||
return matrix; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/g3001_3100/s3033_modify_the_matrix/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
3033\. Modify the Matrix | ||
|
||
Easy | ||
|
||
Given a **0-indexed** `m x n` integer matrix `matrix`, create a new **0-indexed** matrix called `answer`. Make `answer` equal to `matrix`, then replace each element with the value `-1` with the **maximum** element in its respective column. | ||
|
||
Return _the matrix_ `answer`. | ||
|
||
**Example 1:** | ||
|
||
![](https://assets.leetcode.com/uploads/2023/12/24/matrix1.png) | ||
|
||
**Input:** matrix = [[1,2,-1],[4,-1,6],[7,8,9]] | ||
|
||
**Output:** [[1,2,9],[4,8,6],[7,8,9]] | ||
|
||
**Explanation:** The diagram above shows the elements that are changed (in blue). | ||
- We replace the value in the cell [1][1] with the maximum value in the column 1, that is 8. | ||
- We replace the value in the cell [0][2] with the maximum value in the column 2, that is 9. | ||
|
||
**Example 2:** | ||
|
||
![](https://assets.leetcode.com/uploads/2023/12/24/matrix2.png) | ||
|
||
**Input:** matrix = [[3,-1],[5,2]] | ||
|
||
**Output:** [[3,2],[5,2]] | ||
|
||
**Explanation:** The diagram above shows the elements that are changed (in blue). | ||
|
||
**Constraints:** | ||
|
||
* `m == matrix.length` | ||
* `n == matrix[i].length` | ||
* `2 <= m, n <= 50` | ||
* `-1 <= matrix[i][j] <= 100` | ||
* The input is generated such that each column contains at least one non-negative integer. |
30 changes: 30 additions & 0 deletions
30
src/main/java/g3001_3100/s3034_number_of_subarrays_that_match_a_pattern_i/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package g3001_3100.s3034_number_of_subarrays_that_match_a_pattern_i; | ||
|
||
// #Medium #Array #Hash_Function #String_Matching #Rolling_Hash | ||
// #2024_03_01_Time_1_ms_(100.00%)_Space_43.9_MB_(97.20%) | ||
|
||
public class Solution { | ||
public int countMatchingSubarrays(int[] nums, int[] pattern) { | ||
int n = nums.length; | ||
int m = pattern.length; | ||
int count = 0; | ||
for (int i = 0; i <= n - m - 1; i++) { | ||
int k = 0; | ||
while (k < m) { | ||
if (nums[i + k + 1] > nums[i + k] && pattern[k] == 1) { | ||
k++; | ||
} else if (nums[i + k + 1] == nums[i + k] && pattern[k] == 0) { | ||
k++; | ||
} else if (nums[i + k + 1] < nums[i + k] && pattern[k] == -1) { | ||
k++; | ||
} else { | ||
break; | ||
} | ||
} | ||
if (k == m) { | ||
count++; | ||
} | ||
} | ||
return count; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...main/java/g3001_3100/s3034_number_of_subarrays_that_match_a_pattern_i/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
3033\. Modify the Matrix | ||
|
||
Easy | ||
|
||
Given a **0-indexed** `m x n` integer matrix `matrix`, create a new **0-indexed** matrix called `answer`. Make `answer` equal to `matrix`, then replace each element with the value `-1` with the **maximum** element in its respective column. | ||
|
||
Return _the matrix_ `answer`. | ||
|
||
**Example 1:** | ||
|
||
![](https://assets.leetcode.com/uploads/2023/12/24/matrix1.png) | ||
|
||
**Input:** matrix = [[1,2,-1],[4,-1,6],[7,8,9]] | ||
|
||
**Output:** [[1,2,9],[4,8,6],[7,8,9]] | ||
|
||
**Explanation:** The diagram above shows the elements that are changed (in blue). | ||
- We replace the value in the cell [1][1] with the maximum value in the column 1, that is 8. | ||
- We replace the value in the cell [0][2] with the maximum value in the column 2, that is 9. | ||
|
||
**Example 2:** | ||
|
||
![](https://assets.leetcode.com/uploads/2023/12/24/matrix2.png) | ||
|
||
**Input:** matrix = [[3,-1],[5,2]] | ||
|
||
**Output:** [[3,2],[5,2]] | ||
|
||
**Explanation:** The diagram above shows the elements that are changed (in blue). | ||
|
||
**Constraints:** | ||
|
||
* `m == matrix.length` | ||
* `n == matrix[i].length` | ||
* `2 <= m, n <= 50` | ||
* `-1 <= matrix[i][j] <= 100` | ||
* The input is generated such that each column contains at least one non-negative integer. |
51 changes: 51 additions & 0 deletions
51
src/main/java/g3001_3100/s3035_maximum_palindromes_after_operations/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package g3001_3100.s3035_maximum_palindromes_after_operations; | ||
|
||
// #Medium #Array #String #Hash_Table #Sorting #Greedy #Counting | ||
// #2024_03_01_Time_4_ms_(99.13%)_Space_44.9_MB_(90.28%) | ||
|
||
public class Solution { | ||
public int maxPalindromesAfterOperations(String[] words) { | ||
int[] ar = new int[26]; | ||
int[] dp = new int[101]; | ||
int s = 0; | ||
int p = 0; | ||
int ans = 0; | ||
for (String str : words) { | ||
for (char c : str.toCharArray()) { | ||
ar[c - 'a']++; | ||
} | ||
dp[str.length()]++; | ||
} | ||
for (int j : ar) { | ||
s += j % 2; | ||
p += (j / 2); | ||
} | ||
for (int i = 1; i < dp.length; i++) { | ||
if (dp[i] > 0) { | ||
if (i % 2 == 0) { | ||
while (dp[i] > 0 && p > 0) { | ||
p -= i / 2; | ||
if (p >= 0) { | ||
ans++; | ||
} | ||
dp[i]--; | ||
} | ||
} else { | ||
while (dp[i] > 0 && (i == 1 || p > 0)) { | ||
if (s == 0) { | ||
s += 2; | ||
p--; | ||
} | ||
s--; | ||
p -= (i - 1) / 2; | ||
if (p >= 0) { | ||
ans++; | ||
} | ||
dp[i]--; | ||
} | ||
} | ||
} | ||
} | ||
return ans; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/g3001_3100/s3035_maximum_palindromes_after_operations/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
3035\. Maximum Palindromes After Operations | ||
|
||
Medium | ||
|
||
You are given a **0-indexed** string array `words` having length `n` and containing **0-indexed** strings. | ||
|
||
You are allowed to perform the following operation **any** number of times (**including** **zero**): | ||
|
||
* Choose integers `i`, `j`, `x`, and `y` such that `0 <= i, j < n`, `0 <= x < words[i].length`, `0 <= y < words[j].length`, and **swap** the characters `words[i][x]` and `words[j][y]`. | ||
|
||
Return _an integer denoting the **maximum** number of palindromes_ `words` _can contain, after performing some operations._ | ||
|
||
**Note:** `i` and `j` may be equal during an operation. | ||
|
||
**Example 1:** | ||
|
||
**Input:** words = ["abbb","ba","aa"] | ||
|
||
**Output:** 3 | ||
|
||
**Explanation:** In this example, one way to get the maximum number of palindromes is: | ||
|
||
Choose i = 0, j = 1, x = 0, y = 0, so we swap words[0][0] and words[1][0]. words becomes ["bbbb","aa","aa"]. | ||
|
||
All strings in words are now palindromes. Hence, the maximum number of palindromes achievable is 3. | ||
|
||
**Example 2:** | ||
|
||
**Input:** words = ["abc","ab"] | ||
|
||
**Output:** 2 | ||
|
||
**Explanation:** In this example, one way to get the maximum number of palindromes is: | ||
|
||
Choose i = 0, j = 1, x = 1, y = 0, so we swap words[0][1] and words[1][0]. words becomes ["aac","bb"]. | ||
|
||
Choose i = 0, j = 0, x = 1, y = 2, so we swap words[0][1] and words[0][2]. words becomes ["aca","bb"]. | ||
|
||
Both strings are now palindromes. Hence, the maximum number of palindromes achievable is 2. | ||
|
||
**Example 3:** | ||
|
||
**Input:** words = ["cd","ef","a"] | ||
|
||
**Output:** 1 | ||
|
||
**Explanation:** In this example, there is no need to perform any operation. There is one palindrome in words "a". It can be shown that it is not possible to get more than one palindrome after any number of operations. Hence, the answer is 1. | ||
|
||
**Constraints:** | ||
|
||
* `1 <= words.length <= 1000` | ||
* `1 <= words[i].length <= 100` | ||
* `words[i]` consists only of lowercase English letters. |
38 changes: 38 additions & 0 deletions
38
src/main/java/g3001_3100/s3036_number_of_subarrays_that_match_a_pattern_ii/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package g3001_3100.s3036_number_of_subarrays_that_match_a_pattern_ii; | ||
|
||
// #Hard #Array #Hash_Function #String_Matching #Rolling_Hash | ||
// #2024_03_01_Time_5_ms_(98.27%)_Space_172.1_MB_(75.77%) | ||
|
||
public class Solution { | ||
public int countMatchingSubarrays(int[] nums, int[] pattern) { | ||
int n = nums.length; | ||
int m = pattern.length; | ||
int[] arr = new int[n - 1]; | ||
for (int i = 0; i < n - 1; i++) { | ||
if (nums[i + 1] > nums[i]) { | ||
arr[i] = 1; | ||
} else if (nums[i + 1] < nums[i]) { | ||
arr[i] = -1; | ||
} | ||
} | ||
int hash = 0; | ||
int pHash = 0; | ||
int base = 1; | ||
for (int i = 0; i < m; i++) { | ||
hash = hash * 3 + arr[i] + 1; | ||
pHash = pHash * 3 + pattern[i] + 1; | ||
base *= 3; | ||
} | ||
int count = 0; | ||
for (int i = 0; i <= n - 1 - m; i++) { | ||
if (hash == pHash) { | ||
count++; | ||
} | ||
|
||
if (i < n - 1 - m) { | ||
hash = hash * 3 - base * (arr[i] + 1) + arr[i + m] + 1; | ||
} | ||
} | ||
return count; | ||
} | ||
} |
Oops, something went wrong.