LeetCode-in-Java

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

public class Solution {
    public String minimumString(String a, String b, String c) {
        char[] ar = a.toCharArray();
        char[] br = b.toCharArray();
        char[] cr = c.toCharArray();
        return new 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 char[] combine(char[] a, char[] b, char[] c) {
        return combine(combine(a, b), c);
    }

    private char[] combine(char[] a, char[] b) {
        int insertIndex = a.length;
        for (int i = 0; i < a.length; ++i) {
            if (a[i] == b[0]) {
                int ii = i + 1;
                int match = 1;
                for (int j = 1; j < b.length && ii < a.length; j++, ii++) {
                    if (a[ii] == b[j]) {
                        match++;
                    } else {
                        break;
                    }
                }
                if (match == b.length) {
                    return a;
                } else if (match == a.length - i) {
                    insertIndex = i;
                    break;
                }
            }
        }
        char[] tmp = new char[b.length + insertIndex];
        System.arraycopy(a, 0, tmp, 0, insertIndex);
        System.arraycopy(b, 0, tmp, insertIndex, b.length);
        return tmp;
    }

    private char[] getSmaller(char[] res, char[] test) {
        if (res.length > test.length) {
            return test;
        } else if (res.length < test.length) {
            return res;
        } else {
            for (int i = 0; i < res.length; ++i) {
                if (res[i] > test[i]) {
                    return test;
                } else if (res[i] < test[i]) {
                    return res;
                }
            }
        }
        return res;
    }
}