Leetcode Problem 2800. Shortest String That Contains Three Strings

2800. Shortest String That Contains Three Strings

Leetcode Solutions

Try all Possibilities with String Merging

  1. Define a helper function mergeString(s1, s2) that takes two strings and returns the merged string with s2 appended to s1 at the point where s2's prefix matches s1's suffix the most.
  2. Define a function f(A, B, C) that merges strings A, B, and C using the mergeString function.
  3. Initialize a variable finalAns with a string of maximum possible length (e.g., 'a' * 301).
  4. Generate all permutations of the input strings a, b, and c, and for each permutation, use the f function to merge the strings.
  5. Update finalAns with the current merged string if it is shorter than the previous finalAns, or if it is the same length but lexicographically smaller.
  6. Return finalAns as the result.
UML Thumbnail

Permutation and Merge Optimization

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...