LeetCode 1543: Fix Product Name Format Solution

Master LeetCode problem 1543 (Fix Product Name Format), a easy 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.

1543. Fix Product Name Format

Problem Explanation

Explanation

To fix the product name format, we need to ensure that the name is capitalized and does not contain any leading or trailing spaces. We can achieve this by first converting the name to lowercase, then capitalizing the first letter, and finally removing any leading or trailing spaces.

  1. Convert the product name to lowercase.
  2. Capitalize the first letter of the name.
  3. Remove any leading or trailing spaces.

Time complexity: O(n) where n is the length of the product name. Space complexity: O(1)

Solution Code

class Solution {
    public String fixProductName(String name) {
        if (name == null || name.length() == 0) {
            return "";
        }
        
        // Convert name to lowercase
        name = name.toLowerCase();
        
        // Capitalize first letter
        name = name.substring(0, 1).toUpperCase() + name.substring(1);
        
        // Remove leading and trailing spaces
        name = name.trim();
        
        return name;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1543 (Fix Product Name Format)?

This page provides optimized solutions for LeetCode problem 1543 (Fix Product Name Format) 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 1543 (Fix Product Name Format)?

The time complexity for LeetCode 1543 (Fix Product Name Format) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1543 on DevExCode?

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

Back to LeetCode Solutions