Skip to content

Commit 6f4b20d

Browse files
authored
Added tasks 467, 468, 470, 472.
1 parent ac35ac0 commit 6f4b20d

File tree

13 files changed

+430
-0
lines changed

13 files changed

+430
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
16401640
| 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
16411641
| 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
16421642
| 0494 |[Target Sum](src/main/kotlin/g0401_0500/s0494_target_sum/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Backtracking | 308 | 89.61
1643+
| 0472 |[Concatenated Words](src/main/kotlin/g0401_0500/s0472_concatenated_words/Solution.kt)| Hard | Array, String, Dynamic_Programming, Depth_First_Search, Trie | 484 | 100.00
1644+
| 0470 |[Implement Rand10() Using Rand7()](src/main/kotlin/g0401_0500/s0470_implement_rand10_using_rand7/Solution.kt)| Medium | Math, Randomized, Probability_and_Statistics, Rejection_Sampling | 220 | 100.00
1645+
| 0468 |[Validate IP Address](src/main/kotlin/g0401_0500/s0468_validate_ip_address/Solution.kt)| |||
1646+
| 0467 |[Unique Substrings in Wraparound String](src/main/kotlin/g0401_0500/s0467_unique_substrings_in_wraparound_string/Solution.kt)| Medium | String, Dynamic_Programming | 197 | 100.00
16431647
| 0466 |[Count The Repetitions](src/main/kotlin/g0401_0500/s0466_count_the_repetitions/Solution.kt)| Hard | String, Dynamic_Programming | 147 | 100.00
16441648
| 0464 |[Can I Win](src/main/kotlin/g0401_0500/s0464_can_i_win/Solution.kt)| Medium | Dynamic_Programming, Math, Bit_Manipulation, Bitmask, Memoization, Game_Theory | 213 | 100.00
16451649
| 0463 |[Island Perimeter](src/main/kotlin/g0401_0500/s0463_island_perimeter/Solution.kt)| Easy | Array, Depth_First_Search, Breadth_First_Search, Matrix | 381 | 98.04
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0401_0500.s0467_unique_substrings_in_wraparound_string
2+
3+
// #Medium #String #Dynamic_Programming #2022_12_29_Time_197_ms_(100.00%)_Space_35.7_MB_(100.00%)
4+
5+
class Solution {
6+
fun findSubstringInWraproundString(p: String): Int {
7+
val str = p.toCharArray()
8+
val n = str.size
9+
val map = IntArray(26)
10+
var len = 0
11+
for (i in 0 until n) {
12+
if (i > 0 && (str[i - 1].code + 1 == str[i].code || str[i - 1] == 'z' && str[i] == 'a')) {
13+
len += 1
14+
} else {
15+
len = 1
16+
}
17+
// we are storing the max len of string for each letter and then we will count all these
18+
// length.
19+
map[str[i].code - 'a'.code] = Math.max(map[str[i].code - 'a'.code], len)
20+
}
21+
var answer = 0
22+
for (num in map) {
23+
answer += num
24+
}
25+
return answer
26+
}
27+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
467\. Unique Substrings in Wraparound String
2+
3+
Medium
4+
5+
We define the string `base` to be the infinite wraparound string of `"abcdefghijklmnopqrstuvwxyz"`, so `base` will look like this:
6+
7+
* `"...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd...."`.
8+
9+
Given a string `s`, return _the number of **unique non-empty substrings** of_ `s` _are present in_ `base`.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "a"
14+
15+
**Output:** 1
16+
17+
**Explanation:** Only the substring "a" of s is in base.
18+
19+
**Example 2:**
20+
21+
**Input:** s = "cac"
22+
23+
**Output:** 2
24+
25+
**Explanation:** There are two substrings ("a", "c") of s in base.
26+
27+
**Example 3:**
28+
29+
**Input:** s = "zab"
30+
31+
**Output:** 6
32+
33+
**Explanation:** There are six substrings ("z", "a", "b", "za", "ab", and "zab") of s in base.
34+
35+
**Constraints:**
36+
37+
* <code>1 <= s.length <= 10<sup>5</sup></code>
38+
* `s` consists of lowercase English letters.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g0401_0500.s0468_validate_ip_address
2+
3+
// #Medium #String #2022_12_29_Time_192_ms_(62.50%)_Space_35.9_MB_(87.50%)
4+
5+
class Solution {
6+
fun validIPAddress(ip: String): String {
7+
if (ip.length == 0) {
8+
return NEITHER
9+
}
10+
val arr = ip.split("\\.".toRegex()).toTypedArray()
11+
val arr1 = ip.split(":".toRegex()).toTypedArray()
12+
if (arr.size == 4) {
13+
for (num in arr) {
14+
try {
15+
if (num.length > 1 && num.startsWith("0") || num.toInt() > 255) {
16+
return NEITHER
17+
}
18+
} catch (e: Exception) {
19+
return NEITHER
20+
}
21+
}
22+
return "IPv4"
23+
} else if (arr1.size == 8) {
24+
for (num in arr1) {
25+
if (num.length < 1 || num.length > 4) {
26+
return NEITHER
27+
}
28+
for (j in 0 until num.length) {
29+
val ch = num[j]
30+
if (ch.code > 9 &&
31+
(
32+
Character.isLowerCase(ch) && ch > 'f' ||
33+
Character.isUpperCase(ch) && ch > 'F'
34+
)
35+
) {
36+
return NEITHER
37+
}
38+
}
39+
}
40+
return "IPv6"
41+
}
42+
return NEITHER
43+
}
44+
45+
companion object {
46+
private const val NEITHER = "Neither"
47+
}
48+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
468\. Validate IP Address
2+
3+
Medium
4+
5+
Given a string `queryIP`, return `"IPv4"` if IP is a valid IPv4 address, `"IPv6"` if IP is a valid IPv6 address or `"Neither"` if IP is not a correct IP of any type.
6+
7+
**A valid IPv4** address is an IP in the form <code>"x<sub>1</sub>.x<sub>2</sub>.x<sub>3</sub>.x<sub>4</sub>"</code> where <code>0 <= x<sub>i</sub> <= 255</code> and <code>x<sub>i</sub></code> **cannot contain** leading zeros. For example, `"192.168.1.1"` and `"192.168.1.0"` are valid IPv4 addresses while `"192.168.01.1"`, `"192.168.1.00"`, and `"192.168@1.1"` are invalid IPv4 addresses.
8+
9+
**A valid IPv6** address is an IP in the form <code>"x<sub>1</sub>:x<sub>2</sub>:x<sub>3</sub>:x<sub>4</sub>:x<sub>5</sub>:x<sub>6</sub>:x<sub>7</sub>:x<sub>8</sub>"</code> where:
10+
11+
* <code>1 <= x<sub>i</sub>.length <= 4</code>
12+
* <code>x<sub>i</sub></code> is a **hexadecimal string** which may contain digits, lowercase English letter (`'a'` to `'f'`) and upper-case English letters (`'A'` to `'F'`).
13+
* Leading zeros are allowed in <code>x<sub>i</sub></code>.
14+
15+
For example, "`2001:0db8:85a3:0000:0000:8a2e:0370:7334"` and "`2001:db8:85a3:0:0:8A2E:0370:7334"` are valid IPv6 addresses, while "`2001:0db8:85a3::8A2E:037j:7334"` and "`02001:0db8:85a3:0000:0000:8a2e:0370:7334"` are invalid IPv6 addresses.
16+
17+
**Example 1:**
18+
19+
**Input:** queryIP = "172.16.254.1"
20+
21+
**Output:** "IPv4"
22+
23+
**Explanation:** This is a valid IPv4 address, return "IPv4".
24+
25+
**Example 2:**
26+
27+
**Input:** queryIP = "2001:0db8:85a3:0:0:8A2E:0370:7334"
28+
29+
**Output:** "IPv6"
30+
31+
**Explanation:** This is a valid IPv6 address, return "IPv6".
32+
33+
**Example 3:**
34+
35+
**Input:** queryIP = "256.256.256.256"
36+
37+
**Output:** "Neither"
38+
39+
**Explanation:** This is neither a IPv4 address nor a IPv6 address.
40+
41+
**Constraints:**
42+
43+
* `queryIP` consists only of English letters, digits and the characters `'.'` and `':'`.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0401_0500.s0470_implement_rand10_using_rand7
2+
3+
// #Medium #Math #Randomized #Probability_and_Statistics #Rejection_Sampling
4+
// #2022_12_29_Time_220_ms_(100.00%)_Space_38_MB_(80.00%)
5+
6+
import java.util.Random
7+
8+
/*
9+
* The rand7() API is already defined in the parent class SolBase.
10+
* fun rand7(): Int {}
11+
* @return a random integer in the range 1 to 7
12+
*/
13+
@Suppress("kotlin:S2245")
14+
class Solution {
15+
private val random: Random = Random()
16+
fun rand10(): Int {
17+
var r1 = 0
18+
do {
19+
var r2 = 0
20+
do { r2 = rand7() } while (r2 == 7)
21+
if (r2 in 1..3) { r1 = rand7() } else { r1 = 7 + rand7() }
22+
} while (r1 > 10)
23+
return r1
24+
}
25+
26+
private fun rand7(): Int {
27+
return random.nextInt(7) + 1
28+
}
29+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
470\. Implement Rand10() Using Rand7()
2+
3+
Medium
4+
5+
Given the **API** `rand7()` that generates a uniform random integer in the range `[1, 7]`, write a function `rand10()` that generates a uniform random integer in the range `[1, 10]`. You can only call the API `rand7()`, and you shouldn't call any other API. Please **do not** use a language's built-in random API.
6+
7+
Each test case will have one **internal** argument `n`, the number of times that your implemented function `rand10()` will be called while testing. Note that this is **not an argument** passed to `rand10()`.
8+
9+
**Example 1:**
10+
11+
**Input:** n = 1
12+
13+
**Output:** [2]
14+
15+
**Example 2:**
16+
17+
**Input:** n = 2
18+
19+
**Output:** [2,8]
20+
21+
**Example 3:**
22+
23+
**Input:** n = 3
24+
25+
**Output:** [3,8,10]
26+
27+
**Constraints:**
28+
29+
* <code>1 <= n <= 10<sup>5</sup></code>
30+
31+
**Follow up:**
32+
33+
* What is the [expected value](https://en.wikipedia.org/wiki/Expected_value) for the number of calls to `rand7()` function?
34+
* Could you minimize the number of calls to `rand7()`?
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package g0401_0500.s0472_concatenated_words
2+
3+
// #Hard #Array #String #Dynamic_Programming #Depth_First_Search #Trie
4+
// #2022_12_29_Time_484_ms_(100.00%)_Space_48_MB_(100.00%)
5+
6+
import java.util.Arrays
7+
8+
class Solution {
9+
private val ans: MutableList<String> = ArrayList()
10+
private var root: Trie? = null
11+
fun findAllConcatenatedWordsInADict(words: Array<String>): List<String> {
12+
root = Trie()
13+
Arrays.sort(
14+
words
15+
) { a: String, b: String ->
16+
Integer.compare(
17+
a.length,
18+
b.length
19+
)
20+
}
21+
for (word in words) {
22+
var ptr = root
23+
if (search(word, 0, 0)) {
24+
ans.add(word)
25+
} else {
26+
for (j in 0 until word.length) {
27+
if (ptr!!.nxt[word[j].code - 'a'.code] == null) {
28+
ptr.nxt[word[j].code - 'a'.code] = Trie()
29+
}
30+
ptr = ptr.nxt[word[j].code - 'a'.code]
31+
}
32+
ptr!!.endHere = true
33+
}
34+
}
35+
return ans
36+
}
37+
38+
private fun search(cur: String, idx: Int, wordCnt: Int): Boolean {
39+
if (idx == cur.length) {
40+
return wordCnt >= 2
41+
}
42+
var ptr = root
43+
for (i in idx until cur.length) {
44+
if (ptr!!.nxt[cur[i].code - 'a'.code] == null) {
45+
return false
46+
}
47+
if (ptr.nxt[cur[i].code - 'a'.code]!!.endHere) {
48+
val ret = search(cur, i + 1, wordCnt + 1)
49+
if (ret) {
50+
return true
51+
}
52+
}
53+
ptr = ptr.nxt[cur[i].code - 'a'.code]
54+
}
55+
return ptr!!.endHere && wordCnt >= 2
56+
}
57+
58+
private class Trie internal constructor() {
59+
var nxt: Array<Trie?>
60+
var endHere: Boolean
61+
62+
init {
63+
nxt = arrayOfNulls(26)
64+
endHere = false
65+
}
66+
}
67+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
472\. Concatenated Words
2+
3+
Hard
4+
5+
Given an array of strings `words` (**without duplicates**), return _all the **concatenated words** in the given list of_ `words`.
6+
7+
A **concatenated word** is defined as a string that is comprised entirely of at least two shorter words in the given array.
8+
9+
**Example 1:**
10+
11+
**Input:** words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
12+
13+
**Output:** ["catsdogcats","dogcatsdog","ratcatdogcat"]
14+
15+
**Explanation:** "catsdogcats" can be concatenated by "cats", "dog" and "cats"; "dogcatsdog" can be concatenated by "dog", "cats" and "dog"; "ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".
16+
17+
**Example 2:**
18+
19+
**Input:** words = ["cat","dog","catdog"]
20+
21+
**Output:** ["catdog"]
22+
23+
**Constraints:**
24+
25+
* <code>1 <= words.length <= 10<sup>4</sup></code>
26+
* `1 <= words[i].length <= 30`
27+
* `words[i]` consists of only lowercase English letters.
28+
* All the strings of `words` are **unique**.
29+
* <code>1 <= sum(words[i].length) <= 10<sup>5</sup></code>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0401_0500.s0467_unique_substrings_in_wraparound_string
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 findSubstringInWraproundString() {
10+
assertThat(Solution().findSubstringInWraproundString("a"), equalTo(1))
11+
}
12+
13+
@Test
14+
fun findSubstringInWraproundString2() {
15+
assertThat(Solution().findSubstringInWraproundString("cac"), equalTo(2))
16+
}
17+
18+
@Test
19+
fun findSubstringInWraproundString3() {
20+
assertThat(Solution().findSubstringInWraproundString("zab"), equalTo(6))
21+
}
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g0401_0500.s0468_validate_ip_address
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 validIPAddress() {
10+
assertThat(Solution().validIPAddress("172.16.254.1"), equalTo("IPv4"))
11+
}
12+
13+
@Test
14+
fun validIPAddress2() {
15+
assertThat(
16+
Solution().validIPAddress("2001:0db8:85a3:0:0:8A2E:0370:7334"),
17+
equalTo("IPv6")
18+
)
19+
}
20+
21+
@Test
22+
fun validIPAddress3() {
23+
assertThat(Solution().validIPAddress("256.256.256.256"), equalTo("Neither"))
24+
}
25+
}

0 commit comments

Comments
 (0)