Skip to content

Commit b7bebb3

Browse files
committed
Added tasks 17, 18.
1 parent 2740e9b commit b7bebb3

File tree

6 files changed

+234
-0
lines changed

6 files changed

+234
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g0001_0100.s0017_letter_combinations_of_a_phone_number
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Backtracking
4+
5+
class Solution {
6+
fun letterCombinations(digits: String): List<String> {
7+
if (digits.isEmpty()) {
8+
return emptyList()
9+
}
10+
val words: MutableList<String> = ArrayList()
11+
val word = CharArray(digits.length)
12+
helper(digits, word, 0, words)
13+
return words
14+
}
15+
16+
private fun helper(digits: String, word: CharArray, cur: Int, words: MutableList<String>) {
17+
if (cur == digits.length) {
18+
words.add(String(word))
19+
} else {
20+
for (ch in charsForDigit(digits[cur])) {
21+
word[cur] = ch
22+
helper(digits, word, cur + 1, words)
23+
}
24+
}
25+
}
26+
27+
private fun charsForDigit(digit: Char): CharArray {
28+
return when (digit) {
29+
'2' -> charArrayOf('a', 'b', 'c')
30+
'3' -> charArrayOf('d', 'e', 'f')
31+
'4' -> charArrayOf('g', 'h', 'i')
32+
'5' -> charArrayOf('j', 'k', 'l')
33+
'6' -> charArrayOf('m', 'n', 'o')
34+
'7' -> charArrayOf('p', 'q', 'r', 's')
35+
'8' -> charArrayOf('t', 'u', 'v')
36+
'9' -> charArrayOf('w', 'x', 'y', 'z')
37+
else -> charArrayOf()
38+
}
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
17\. Letter Combinations of a Phone Number
2+
3+
Medium
4+
5+
Given a string containing digits from `2-9` inclusive, return all possible letter combinations that the number could represent. Return the answer in **any order**.
6+
7+
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
8+
9+
![](https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Telephone-keypad2.svg/200px-Telephone-keypad2.svg.png)
10+
11+
**Example 1:**
12+
13+
**Input:** digits = "23"
14+
15+
**Output:** ["ad","ae","af","bd","be","bf","cd","ce","cf"]
16+
17+
**Example 2:**
18+
19+
**Input:** digits = ""
20+
21+
**Output:** []
22+
23+
**Example 3:**
24+
25+
**Input:** digits = "2"
26+
27+
**Output:** ["a","b","c"]
28+
29+
**Constraints:**
30+
31+
* `0 <= digits.length <= 4`
32+
* `digits[i]` is a digit in the range `['2', '9']`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package g0001_0100.s0018_4sum
2+
3+
// #Medium #Array #Sorting #Two_Pointers
4+
5+
import java.util.Arrays
6+
7+
class Solution {
8+
fun fourSum(nums: IntArray, target: Int): List<List<Int>> {
9+
val list: MutableList<List<Int>> = ArrayList()
10+
Arrays.sort(nums)
11+
val n = nums.size
12+
if (n == 0) {
13+
return list
14+
}
15+
for (a in 0 until n - 3) {
16+
if (a > 0 && nums[a] == nums[a - 1]) {
17+
continue
18+
}
19+
for (d in n - 1 downTo a + 2 + 1) {
20+
if (d < n - 1 && nums[d] == nums[d + 1]) {
21+
continue
22+
}
23+
var b = a + 1
24+
var c = d - 1
25+
val min = nums[a] + nums[d] + nums[b] + nums[b + 1]
26+
if (min > target) {
27+
continue
28+
}
29+
val max = nums[a] + nums[d] + nums[c] + nums[c - 1]
30+
if (max < target) {
31+
break
32+
}
33+
while (c > b) {
34+
val sum = nums[a] + nums[b] + nums[c] + nums[d]
35+
if (sum > target) {
36+
c--
37+
} else if (sum < target) {
38+
b++
39+
} else {
40+
list.add(Arrays.asList(nums[a], nums[b], nums[c], nums[d]))
41+
while (c > b && nums[b] == nums[b + 1]) {
42+
b++
43+
}
44+
while (c > b && nums[c] == nums[c - 1]) {
45+
c--
46+
}
47+
b++
48+
c--
49+
}
50+
}
51+
}
52+
}
53+
return list
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
18\. 4Sum
2+
3+
Medium
4+
5+
Given an array `nums` of `n` integers, return _an array of all the **unique** quadruplets_ `[nums[a], nums[b], nums[c], nums[d]]` such that:
6+
7+
* `0 <= a, b, c, d < n`
8+
* `a`, `b`, `c`, and `d` are **distinct**.
9+
* `nums[a] + nums[b] + nums[c] + nums[d] == target`
10+
11+
You may return the answer in **any order**.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,0,-1,0,-2,2], target = 0
16+
17+
**Output:** [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [2,2,2,2,2], target = 8
22+
23+
**Output:** [[2,2,2,2]]
24+
25+
**Constraints:**
26+
27+
* `1 <= nums.length <= 200`
28+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
29+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package g0001_0100.s0017_letter_combinations_of_a_phone_number
2+
3+
import org.hamcrest.CoreMatchers
4+
import org.hamcrest.MatcherAssert
5+
import org.junit.jupiter.api.Test
6+
import java.util.Arrays
7+
8+
internal class SolutionTest {
9+
@Test
10+
fun letterCombinations() {
11+
MatcherAssert.assertThat(
12+
Solution().letterCombinations("23"),
13+
CoreMatchers.equalTo(Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"))
14+
)
15+
}
16+
17+
@Test
18+
fun letterCombinations2() {
19+
MatcherAssert.assertThat(Solution().letterCombinations(""), CoreMatchers.equalTo(Arrays.asList<Any>()))
20+
}
21+
22+
@Test
23+
fun letterCombinations3() {
24+
MatcherAssert.assertThat(Solution().letterCombinations("2"), CoreMatchers.equalTo(Arrays.asList("a", "b", "c")))
25+
}
26+
27+
@Test
28+
fun letterCombinations4() {
29+
MatcherAssert.assertThat(Solution().letterCombinations("4"), CoreMatchers.equalTo(Arrays.asList("g", "h", "i")))
30+
}
31+
32+
@Test
33+
fun letterCombinations5() {
34+
MatcherAssert.assertThat(Solution().letterCombinations("5"), CoreMatchers.equalTo(Arrays.asList("j", "k", "l")))
35+
}
36+
37+
@Test
38+
fun letterCombinations6() {
39+
MatcherAssert.assertThat(Solution().letterCombinations("6"), CoreMatchers.equalTo(Arrays.asList("m", "n", "o")))
40+
}
41+
42+
@Test
43+
fun letterCombinations7() {
44+
MatcherAssert.assertThat(
45+
Solution().letterCombinations("7"), CoreMatchers.equalTo(Arrays.asList("p", "q", "r", "s"))
46+
)
47+
}
48+
49+
@Test
50+
fun letterCombinations8() {
51+
MatcherAssert.assertThat(Solution().letterCombinations("8"), CoreMatchers.equalTo(Arrays.asList("t", "u", "v")))
52+
}
53+
54+
@Test
55+
fun letterCombinations9() {
56+
MatcherAssert.assertThat(
57+
Solution().letterCombinations("9"), CoreMatchers.equalTo(Arrays.asList("w", "x", "y", "z"))
58+
)
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0001_0100.s0018_4sum
2+
3+
import com_github_leetcode.ArrayUtils.getLists
4+
import org.hamcrest.CoreMatchers.equalTo
5+
import org.hamcrest.MatcherAssert.assertThat
6+
import org.junit.jupiter.api.Test
7+
8+
internal class SolutionTest {
9+
@Test
10+
fun fourSum() {
11+
assertThat(
12+
Solution().fourSum(intArrayOf(1, 0, -1, 0, -2, 2), 0),
13+
equalTo(
14+
getLists(arrayOf(intArrayOf(-2, -1, 1, 2), intArrayOf(-2, 0, 0, 2), intArrayOf(-1, 0, 0, 1)))
15+
)
16+
)
17+
}
18+
}

0 commit comments

Comments
 (0)