LeetCode 3253: Construct String with Minimum Cost (Easy) Solution

Master LeetCode problem 3253 (Construct String with Minimum Cost (Easy)), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3253. Construct String with Minimum Cost (Easy)

Medium

Problem Explanation

Explanation:

To solve this problem, we can use a greedy approach. We start with an empty string and iteratively add characters from the input strings based on the cost of adding each character. We always choose the character with the minimum cost to add at each step.

  1. Initialize an empty string result to store the constructed string.
  2. Iterate through the input strings and for each character:
    • If the cost of adding the character to the result string is less than the cost of adding the next character, add the character to the result string.
    • Otherwise, skip adding the character.
  3. Return the final constructed string.

Time Complexity: O(n), where n is the total number of characters in the input strings. Space Complexity: O(1) since we are only using constant extra space.

:

Solution Code

class Solution {
    public String constructString(String[] strs, int[] costs) {
        StringBuilder result = new StringBuilder();
        
        for (int i = 0; i < strs.length; i++) {
            if (result.length() == 0) {
                result.append(strs[i]);
            } else {
                String currStr = strs[i];
                int currCost = costs[i];
                
                int insertIdx = 0;
                int minCost = costs[i];
                for (int j = 0; j < result.length(); j++) {
                    int totalCost = currCost + costs[j];
                    if (totalCost < minCost) {
                        minCost = totalCost;
                        insertIdx = j;
                    }
                }
                
                result.insert(insertIdx, currStr);
            }
        }
        
        return result.toString();
    }
}

Try It Yourself

Loading code editor...

Frequently Asked Questions

How to solve LeetCode 3253 (Construct String with Minimum Cost (Easy))?

This page provides optimized solutions for LeetCode problem 3253 (Construct String with Minimum Cost (Easy)) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3253 (Construct String with Minimum Cost (Easy))?

The time complexity for LeetCode 3253 (Construct String with Minimum Cost (Easy)) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3253 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3253 in Java, C++, or Python.

Back to LeetCode Solutions