File tree Expand file tree Collapse file tree 4 files changed +101
-0
lines changed
main/kotlin/com/schmoczer/leetcode/_0383
test/kotlin/com/schmoczer/leetcode/_0383 Expand file tree Collapse file tree 4 files changed +101
-0
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ My solutions to LeetCode problems in Kotlin.
21
21
| [ 186] ( https://leetcode.com/problems/reverse-words-in-a-string-ii/ ) | [ Reverse Words in a String II] ( src/main/kotlin/com/schmoczer/leetcode/_0186/ReverseWordsInStringInPlace.kt ) | Medium |
22
22
| [ 206] ( https://leetcode.com/problems/reverse-linked-list/ ) | [ Reverse Linked List] ( src/main/kotlin/com/schmoczer/leetcode/_0206/ReverseLinkedList.kt ) | Easy |
23
23
| [ 344] ( https://leetcode.com/problems/reverse-string/ ) | [ Reverse String] ( src/main/kotlin/com/schmoczer/leetcode/_0344/ReverseString.kt ) | Easy |
24
+ | [ 383] ( https://leetcode.com/problems/ransom-note/ ) | [ Ransom Note] ( src/main/kotlin/com/schmoczer/leetcode/_0383/RansomNote.kt ) | Easy |
24
25
| [ 412] ( https://leetcode.com/problems/fizz-buzz/ ) | [ Fizz Buzz] ( src/main/kotlin/com/schmoczer/leetcode/_0412/FizzBuzz.kt ) | Easy |
25
26
| [ 773] ( https://leetcode.com/problems/sliding-puzzle/ ) | [ Sliding Puzzle] ( src/main/kotlin/com/schmoczer/leetcode/_0773/SlidingPuzzle.kt ) | Hard |
26
27
| [ 796] ( https://leetcode.com/problems/rotate-string/ ) | [ Rotate String] ( src/main/kotlin/com/schmoczer/leetcode/_0796/RotateString.kt ) | Easy |
Original file line number Diff line number Diff line change
1
+ # Ransom Note
2
+
3
+ Given two strings ` ransomNote ` and ` magazine ` , return ` true ` if ` ransomNote ` can be constructed by using the letters
4
+ from ` magazine ` and ` false ` otherwise.
5
+
6
+ Each letter in ` magazine ` can only be used once in ` ransomNote ` .
7
+
8
+ Example 1:
9
+
10
+ > Input: ransomNote = "a", magazine = "b"
11
+ >
12
+ > Output: false
13
+
14
+ Example 2:
15
+
16
+ > Input: ransomNote = "aa", magazine = "ab"
17
+ >
18
+ > Output: false
19
+
20
+ Example 3:
21
+
22
+ > Input: ransomNote = "aa", magazine = "aab"
23
+ >
24
+ > Output: true
25
+
26
+ Constraints:
27
+
28
+ - ` 1 <= ransomNote.length, magazine.length <= 10^5 `
29
+ - ` ransomNote ` and ` magazine ` consist of lowercase English letters.
Original file line number Diff line number Diff line change
1
+ package com.schmoczer.leetcode._0383
2
+
3
+ class RansomNote {
4
+ // Approach 1: remove letters from magazine, simple & intuitive
5
+ fun canConstruct1 (ransomNote : String , magazine : String ): Boolean {
6
+ val magazine = magazine.toMutableList()
7
+ for (letter in ransomNote) {
8
+ if (! magazine.remove(letter)) {
9
+ return false
10
+ }
11
+ }
12
+ return true
13
+ }
14
+
15
+ // Approach 2: map occurrences of a letter into a IntArray
16
+ // Runtime 3ms Beats 98.51%
17
+ fun canConstruct (ransomNote : String , magazine : String ): Boolean {
18
+ val alphabet = IntArray (123 )
19
+ for (letter in magazine) {
20
+ alphabet[letter.code]++
21
+ }
22
+
23
+ for (letter in ransomNote) {
24
+ if (alphabet[letter.code] == 0 ) {
25
+ return false
26
+ }
27
+ alphabet[letter.code]--
28
+ }
29
+
30
+ return true
31
+ }
32
+ }
Original file line number Diff line number Diff line change
1
+ package com.schmoczer.leetcode._0383
2
+
3
+ import org.junit.jupiter.api.Assertions.assertFalse
4
+ import org.junit.jupiter.api.Assertions.assertTrue
5
+ import org.junit.jupiter.api.BeforeEach
6
+ import kotlin.test.Test
7
+
8
+ class RansomNoteTest {
9
+ private lateinit var sut: RansomNote
10
+
11
+ @BeforeEach
12
+ fun setUp () {
13
+ sut = RansomNote ()
14
+ }
15
+
16
+ @Test
17
+ fun `a can not be constructed from b` () {
18
+ val note = " a"
19
+ val magazine = " b"
20
+
21
+ assertFalse(sut.canConstruct(note, magazine))
22
+ }
23
+
24
+ @Test
25
+ fun `aa can not be constructed from ab` () {
26
+ val note = " aa"
27
+ val magazine = " ab"
28
+
29
+ assertFalse(sut.canConstruct(note, magazine))
30
+ }
31
+
32
+ @Test
33
+ fun `aa can be constructed from aab` () {
34
+ val note = " aa"
35
+ val magazine = " aab"
36
+
37
+ assertTrue(sut.canConstruct(note, magazine))
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments