Skip to content

Commit 1df9e07

Browse files
author
Karolina Kafel
committed
Append And Delete - HR - E
1 parent 4bfb348 commit 1df9e07

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.hackerrank
2+
3+
import java.util.*
4+
5+
fun main(args: Array<String>) {
6+
val scan = Scanner(System.`in`)
7+
val s = scan.next()
8+
val t = scan.next()
9+
val k = scan.nextInt()
10+
11+
println(if (canTransform(s, t, k)) "Yes" else "No")
12+
}
13+
14+
fun canTransform(s: String, t: String, k: Int): Boolean {
15+
val commonPrefix = t.commonPrefixWith(s)
16+
val diff = t.length + s.length - commonPrefix.length * 2
17+
return k >= diff && (diff % 2 == k % 2) || s.length + t.length < k
18+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.hackerrank
2+
3+
import org.junit.Test
4+
import kotlin.test.assertFalse
5+
import kotlin.test.assertTrue
6+
7+
class AppendAndDeleteKtTest {
8+
9+
@Test
10+
fun shouldBeAbleToTransform() {
11+
assertTrue(canTransform("hackerhappy", "hackerrank", 9))
12+
}
13+
14+
@Test
15+
fun shouldBeAbleToTransformToo() {
16+
assertTrue(canTransform("aba", "aba", 7))
17+
}
18+
19+
@Test
20+
fun shouldBeAbleToTransformToo2() {
21+
assertTrue(canTransform("zzzzz", "zzzzzzz", 4))
22+
}
23+
24+
@Test
25+
fun shouldBeAbleToTransformToo3() {
26+
assertTrue(canTransform("asdfqwertyuighjkzxcvasdfqwertyuighjkzxcvasdfqwertyuighjkzxcvasdfqwertyuighjkzxcvasdfqwertyuighjkzxcv", "asdfqwertyuighjkzxcvasdfqwertyuighjkzxcvasdfqwertyuighjkzxcvasdfqwertyuighjkzxcvasdfqwertyuighjkzxcv", 20))
27+
}
28+
29+
@Test
30+
fun shouldBeAbleToTransformToo4() {
31+
assertTrue(canTransform("aaa", "a", 5))
32+
}
33+
34+
@Test
35+
fun shouldBeAbleToTransformToo5() {
36+
assertTrue(canTransform("abcdef", "fedcba", 15))
37+
}
38+
39+
@Test
40+
fun shouldNotBeAbleToTransform() {
41+
assertFalse(canTransform("abcd", "abcdert", 10))
42+
}
43+
44+
@Test
45+
fun shouldNotBeAbleToTransformToo() {
46+
assertFalse(canTransform("qwerasdf", "qwerbsdf", 6))
47+
}
48+
}

0 commit comments

Comments
 (0)