Skip to content

Commit c3bc8bc

Browse files
authored
Added tasks 522, 523, 524, 525.
1 parent 2eb4aa6 commit c3bc8bc

File tree

13 files changed

+341
-0
lines changed

13 files changed

+341
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,6 +1647,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
16471647
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
16481648
| 0560 |[Subarray Sum Equals K](src/main/kotlin/g0501_0600/s0560_subarray_sum_equals_k/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Data_Structure_II_Day_5_Array | 692 | 53.27
16491649
| 0543 |[Diameter of Binary Tree](src/main/kotlin/g0501_0600/s0543_diameter_of_binary_tree/Solution.kt)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Udemy_Tree_Stack_Queue | 307 | 43.93
1650+
| 0525 |[Contiguous Array](src/main/kotlin/g0501_0600/s0525_contiguous_array/Solution.kt)| Medium | Array, Hash_Table, Prefix_Sum | 471 | 100.00
1651+
| 0524 |[Longest Word in Dictionary through Deleting](src/main/kotlin/g0501_0600/s0524_longest_word_in_dictionary_through_deleting/Solution.kt)| Medium | Array, String, Sorting, Two_Pointers | 307 | 100.00
1652+
| 0523 |[Continuous Subarray Sum](src/main/kotlin/g0501_0600/s0523_continuous_subarray_sum/Solution.kt)| Medium | Array, Hash_Table, Math, Prefix_Sum | 682 | 95.45
1653+
| 0522 |[Longest Uncommon Subsequence II](src/main/kotlin/g0501_0600/s0522_longest_uncommon_subsequence_ii/Solution.kt)| Medium | Array, String, Hash_Table, Sorting, Two_Pointers | 163 | 100.00
16501654
| 0521 |[Longest Uncommon Subsequence I](src/main/kotlin/g0501_0600/s0521_longest_uncommon_subsequence_i/Solution.kt)| Easy | String | 146 | 88.89
16511655
| 0520 |[Detect Capital](src/main/kotlin/g0501_0600/s0520_detect_capital/Solution.kt)| Easy | String | 161 | 84.54
16521656
| 0519 |[Random Flip Matrix](src/main/kotlin/g0501_0600/s0519_random_flip_matrix/Solution.kt)| Medium | Hash_Table, Math, Randomized, Reservoir_Sampling | 270 | 100.00
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g0501_0600.s0522_longest_uncommon_subsequence_ii
2+
3+
// #Medium #Array #String #Hash_Table #Sorting #Two_Pointers
4+
// #2023_01_14_Time_163_ms_(100.00%)_Space_33.6_MB_(50.00%)
5+
6+
class Solution {
7+
fun findLUSlength(strs: Array<String>): Int {
8+
var maxUncommonLen = -1
9+
for (indx1 in strs.indices) {
10+
var isCommon = false
11+
for (indx2 in strs.indices) {
12+
if (indx2 != indx1 && isSubSequence(strs[indx1], strs[indx2])) {
13+
isCommon = true
14+
break
15+
}
16+
}
17+
if (!isCommon) {
18+
maxUncommonLen = Math.max(maxUncommonLen, strs[indx1].length)
19+
}
20+
}
21+
return maxUncommonLen
22+
}
23+
24+
private fun isSubSequence(str1: String, str2: String): Boolean {
25+
var indx1 = 0
26+
for (indx2 in 0 until str2.length) {
27+
if (str1[indx1] == str2[indx2]) {
28+
indx1++
29+
}
30+
if (indx1 == str1.length) {
31+
return true
32+
}
33+
}
34+
return indx1 == str1.length
35+
}
36+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
522\. Longest Uncommon Subsequence II
2+
3+
Medium
4+
5+
Given an array of strings `strs`, return _the length of the **longest uncommon subsequence** between them_. If the longest uncommon subsequence does not exist, return `-1`.
6+
7+
An **uncommon subsequence** between an array of strings is a string that is a **subsequence of one string but not the others**.
8+
9+
A **subsequence** of a string `s` is a string that can be obtained after deleting any number of characters from `s`.
10+
11+
* For example, `"abc"` is a subsequence of `"aebdc"` because you can delete the underlined characters in <code>"a<ins>e</ins>b<ins>d</ins>c"</code> to get `"abc"`. Other subsequences of `"aebdc"` include `"aebdc"`, `"aeb"`, and `""` (empty string).
12+
13+
**Example 1:**
14+
15+
**Input:** strs = ["aba","cdc","eae"]
16+
17+
**Output:** 3
18+
19+
**Example 2:**
20+
21+
**Input:** strs = ["aaa","aaa","aa"]
22+
23+
**Output:** -1
24+
25+
**Constraints:**
26+
27+
* `2 <= strs.length <= 50`
28+
* `1 <= strs[i].length <= 10`
29+
* `strs[i]` consists of lowercase English letters.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0501_0600.s0523_continuous_subarray_sum
2+
3+
// #Medium #Array #Hash_Table #Math #Prefix_Sum
4+
// #2023_01_14_Time_682_ms_(95.45%)_Space_55_MB_(86.36%)
5+
6+
class Solution {
7+
fun checkSubarraySum(nums: IntArray, k: Int): Boolean {
8+
val map: MutableMap<Int, Int> = HashMap()
9+
var sum = 0
10+
map[0] = -1
11+
for (i in nums.indices) {
12+
sum += nums[i]
13+
val remainder = sum % k
14+
if (map.containsKey(remainder)) {
15+
if (map[remainder]!! + 1 < i) {
16+
return true
17+
}
18+
} else {
19+
map[remainder] = i
20+
}
21+
}
22+
return false
23+
}
24+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
523\. Continuous Subarray Sum
2+
3+
Medium
4+
5+
Given an integer array nums and an integer k, return `true` _if_ `nums` _has a **good subarray** or_ `false` _otherwise_.
6+
7+
A **good subarray** is a subarray where:
8+
9+
* its length is **at least two**, and
10+
* the sum of the elements of the subarray is a multiple of `k`.
11+
12+
**Note** that:
13+
14+
* A **subarray** is a contiguous part of the array.
15+
* An integer `x` is a multiple of `k` if there exists an integer `n` such that `x = n * k`. `0` is **always** a multiple of `k`.
16+
17+
**Example 1:**
18+
19+
**Input:** nums = [23,<ins>2,4</ins>,6,7], k = 6
20+
21+
**Output:** true
22+
23+
**Explanation:** [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [<ins>23,2,6,4,7</ins>], k = 6
28+
29+
**Output:** true
30+
31+
**Explanation:** [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42. 42 is a multiple of 6 because 42 = 7 \* 6 and 7 is an integer.
32+
33+
**Example 3:**
34+
35+
**Input:** nums = [23,2,6,4,7], k = 13
36+
37+
**Output:** false
38+
39+
**Constraints:**
40+
41+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
42+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
43+
* <code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code>
44+
* <code>1 <= k <= 2<sup>31</sup> - 1</code>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g0501_0600.s0524_longest_word_in_dictionary_through_deleting
2+
3+
// #Medium #Array #String #Sorting #Two_Pointers
4+
// #2023_01_14_Time_307_ms_(100.00%)_Space_38.4_MB_(100.00%)
5+
6+
import java.util.ArrayDeque
7+
import java.util.Deque
8+
9+
class Solution {
10+
private class Pair(var word: String, var index: Int)
11+
12+
fun findLongestWord(s: String, dictionary: List<String>): String {
13+
val map: MutableMap<Char, Deque<Pair?>> = HashMap()
14+
var c = 'a'
15+
while (c <= 'z') {
16+
map[c] = ArrayDeque()
17+
c++
18+
}
19+
for (word in dictionary) {
20+
map[word[0]]!!.offerFirst(Pair(word, 0))
21+
}
22+
var maxLen = 0
23+
var res = ""
24+
for (i in 0 until s.length) {
25+
if (!map[s[i]]!!.isEmpty()) {
26+
val deque = map[s[i]]!!
27+
val size = deque.size
28+
for (j in 0 until size) {
29+
val pair = deque.pollLast()!!
30+
if (pair.index == pair.word.length - 1) {
31+
if (maxLen < pair.word.length) {
32+
maxLen = pair.word.length
33+
res = pair.word
34+
} else if (maxLen == pair.word.length && res.compareTo(pair.word) > 0) {
35+
res = pair.word
36+
}
37+
} else {
38+
pair.index++
39+
map[pair.word[pair.index]]!!.offerFirst(pair)
40+
}
41+
}
42+
}
43+
}
44+
return res
45+
}
46+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
524\. Longest Word in Dictionary through Deleting
2+
3+
Medium
4+
5+
Given a string `s` and a string array `dictionary`, return _the longest string in the dictionary that can be formed by deleting some of the given string characters_. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "abpcplea", dictionary = ["ale","apple","monkey","plea"]
10+
11+
**Output:** "apple"
12+
13+
**Example 2:**
14+
15+
**Input:** s = "abpcplea", dictionary = ["a","b","c"]
16+
17+
**Output:** "a"
18+
19+
**Constraints:**
20+
21+
* `1 <= s.length <= 1000`
22+
* `1 <= dictionary.length <= 1000`
23+
* `1 <= dictionary[i].length <= 1000`
24+
* `s` and `dictionary[i]` consist of lowercase English letters.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g0501_0600.s0525_contiguous_array
2+
3+
// #Medium #Array #Hash_Table #Prefix_Sum #2023_01_14_Time_471_ms_(100.00%)_Space_45.9_MB_(83.33%)
4+
5+
class Solution {
6+
fun findMaxLength(nums: IntArray): Int {
7+
for (i in nums.indices) {
8+
if (nums[i] == 0) {
9+
nums[i] = -1
10+
}
11+
}
12+
val map: HashMap<Int, Int> = HashMap()
13+
map[0] = -1
14+
var ps = 0
15+
var len = 0
16+
for (i in nums.indices) {
17+
ps += nums[i]
18+
if (!map.containsKey(ps)) {
19+
map[ps] = i
20+
} else {
21+
len = Math.max(len, i - map[ps]!!)
22+
}
23+
}
24+
return len
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
525\. Contiguous Array
2+
3+
Medium
4+
5+
Given a binary array `nums`, return _the maximum length of a contiguous subarray with an equal number of_ `0` _and_ `1`.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [0,1]
10+
11+
**Output:** 2
12+
13+
**Explanation:** [0, 1] is the longest contiguous subarray with an equal number of 0 and 1.
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [0,1,0]
18+
19+
**Output:** 2
20+
21+
**Explanation:** [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.
22+
23+
**Constraints:**
24+
25+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
26+
* `nums[i]` is either `0` or `1`.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0501_0600.s0522_longest_uncommon_subsequence_ii
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun findLUSlength() {
10+
assertThat(Solution().findLUSlength(arrayOf("aba", "cdc", "eae")), equalTo(3))
11+
}
12+
13+
@Test
14+
fun findLUSlength2() {
15+
assertThat(Solution().findLUSlength(arrayOf("aaa", "aaa", "aa")), equalTo(-1))
16+
}
17+
}

0 commit comments

Comments
 (0)