LeetCode in Kotlin

2800. Shortest String That Contains Three Strings

Medium

Given three strings a, b, and c, your task is to find a string that has the minimum length and contains all three strings as substrings.

If there are multiple such strings, return the lexicographically smallest one.

Return a string denoting the answer to the problem.

Notes

Example 1:

Input: a = “abc”, b = “bca”, c = “aaa”

Output: “aaabca”

Explanation: We show that “aaabca” contains all the given strings: a = ans[2…4], b = ans[3..5], c = ans[0..2]. It can be shown that the length of the resulting string would be at least 6 and “aaabca” is the lexicographically smallest one.

Example 2:

Input: a = “ab”, b = “ba”, c = “aba”

Output: “aba”

Explanation: We show that the string “aba” contains all the given strings: a = ans[0..1], b = ans[1..2], c = ans[0..2]. Since the length of c is 3, the length of the resulting string would be at least 3. It can be shown that “aba” is the lexicographically smallest one.

Constraints:

Solution

class Solution {
    fun minimumString(a: String, b: String, c: String): String {
        val ar = a.toCharArray()
        val br = b.toCharArray()
        val cr = c.toCharArray()
        return String(
            getSmaller(
                combine(ar, br, cr),
                getSmaller(
                    combine(ar, cr, br),
                    getSmaller(
                        combine(br, ar, cr),
                        getSmaller(
                            combine(br, cr, ar),
                            getSmaller(combine(cr, ar, br), combine(cr, br, ar))
                        )
                    )
                )
            )
        )
    }

    private fun combine(a: CharArray, b: CharArray, c: CharArray): CharArray {
        return combine(combine(a, b), c)
    }

    private fun combine(a: CharArray, b: CharArray): CharArray {
        var insertIndex = a.size
        for (i in a.indices) {
            if (a[i] == b[0]) {
                var ii = i + 1
                var match = 1
                var j = 1
                while (j < b.size && ii < a.size) {
                    if (a[ii] == b[j]) match++ else break
                    ii++
                    ++j
                }
                if (match == b.size) {
                    return a
                } else if (match == a.size - i) {
                    insertIndex = i
                    break
                }
            }
        }
        val tmp = CharArray(b.size + insertIndex)
        for (i in 0 until insertIndex) tmp[i] = a[i]
        for (i in b.indices) tmp[i + insertIndex] = b[i]
        return tmp
    }

    private fun getSmaller(res: CharArray, test: CharArray): CharArray {
        if (res.size > test.size) return test else if (res.size < test.size) return res else {
            for (i in res.indices) {
                if (res[i] > test[i]) return test else if (res[i] < test[i]) return res
            }
        }
        return res
    }
}