Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9bef63f

Browse files
383: Ransom Note
1 parent 8d2f7e2 commit 9bef63f

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ My solutions to LeetCode problems in Kotlin.
2121
| [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 |
2222
| [206](https://leetcode.com/problems/reverse-linked-list/) | [Reverse Linked List](src/main/kotlin/com/schmoczer/leetcode/_0206/ReverseLinkedList.kt) | Easy |
2323
| [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 |
2425
| [412](https://leetcode.com/problems/fizz-buzz/) | [Fizz Buzz](src/main/kotlin/com/schmoczer/leetcode/_0412/FizzBuzz.kt) | Easy |
2526
| [773](https://leetcode.com/problems/sliding-puzzle/) | [Sliding Puzzle](src/main/kotlin/com/schmoczer/leetcode/_0773/SlidingPuzzle.kt) | Hard |
2627
| [796](https://leetcode.com/problems/rotate-string/) | [Rotate String](src/main/kotlin/com/schmoczer/leetcode/_0796/RotateString.kt) | Easy |
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /