Decremental String Concatenation - Problem

You are given a 0-indexed array words containing n strings.

Let's define a join operation join(x, y) between two strings x and y as concatenating them into xy. However, if the last character of x is equal to the first character of y, one of them is deleted.

For example: join("ab", "ba") = "aba" and join("ab", "cde") = "abcde".

You are to perform n - 1 join operations. Let str0 = words[0]. Starting from i = 1 up to i = n - 1, for the ith operation, you can do one of the following:

  • Make stri = join(stri - 1, words[i])
  • Make stri = join(words[i], stri - 1)

Your task is to minimize the length of strn - 1.

Return an integer denoting the minimum possible length of strn - 1.

Input & Output

Example 1 — Basic Case
$ Input: words = ["aa", "ab", "bc"]
Output: 4
💡 Note: Join "aa" + "ab" = "aab" (no overlap), then "aab" + "bc" = "aabc" (b matches, one deleted) = length 4. Alternative: "aa" + "bc" + "ab" would give length 6, so 4 is optimal.
Example 2 — With Overlap
$ Input: words = ["ab", "ba"]
Output: 3
💡 Note: join("ab", "ba") = "aba" because last char of "ab" (b) equals first char of "ba" (b), so one is deleted. Length = 3.
Example 3 — No Overlap
$ Input: words = ["abc", "def"]
Output: 6
💡 Note: No characters match, so join("abc", "def") = "abcdef" with length 6. Order doesn't matter here.

Constraints

  • 2 ≤ words.length ≤ 10
  • 1 ≤ words[i].length ≤ 50
  • words[i] consists of lowercase English letters only

Visualization

Tap to expand
Decremental String Concatenation INPUT words array (n=3) "aa" i=0 "ab" i=1 "bc" i=2 Join Operation: join(x, y) = xy If last(x) == first(y): one char deleted Example: join("ab","ba") = "aba" join("ab","cde") = "abcde" Goal: Minimize length of final string str(n-1) n-1 = 2 operations ALGORITHM STEPS 1 Initialize str0 = "aa" (len=2) first='a', last='a' 2 i=1: Join with "ab" Option A: join("aa","ab") = "aab" (len=3) [OK] Option B: join("ab","aa") = "aba" (len=3) 3 i=2: Join with "bc" From "aab": last='b' join("aab","bc")="aabc" (len=4, b==b saves 1) 4 DP State Track: (first_char, last_char) dp[i][f][l] = min length with first=f, last=l Optimal Path: "aa" --> "aab" --> "aabc" len: 2 --> 3 --> 4 FINAL RESULT Final String str2: "aabc" Character Breakdown: a a b c OUTPUT 4 Minimum possible length of final joined string Saved 2 chars via overlaps! Key Insight: Use Dynamic Programming tracking only the first and last characters of the current string. State: dp[i][first_char][last_char] = minimum length. At each step, choose to prepend or append the next word, maximizing character overlaps. Time: O(n * 26 * 26), Space: O(26 * 26). TutorialsPoint - Decremental String Concatenation | Optimal Solution
Asked in
Google 15 Microsoft 12 Amazon 8
23.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen