Facebook Pixel

3374. First Letter Capitalization II

HardDatabase
Leetcode Link

Problem Description

We are given a table named user_content with two columns: content_id and content_text. The goal is to transform the text in the content_text column by applying the following rules:

  1. Convert the first letter of each word to uppercase and the remaining letters to lowercase.
  2. Special handling for words containing special characters:
    • For words connected with a hyphen -, both parts should be capitalized (e.g., top-rated becomes Top-Rated).
  3. All other formatting and spacing should remain unchanged.

The solution should return a result table that includes both the original content_text and the modified text following the above rules.

Intuition

To address this problem, we need to process each word within content_text by adhering to specific transformation rules. The approach involves iterating through each word, adjusting its capitalization, and preserving the original spacing and formatting.

Here's a step-by-step breakdown of the solution approach:

  1. Split the Text: Break the content_text into individual words. This allows us to handle each word separately.
  2. Handle Hyphenated Words: For words containing hyphens, split the word on the hyphen and capitalize both parts. This ensures words like quick-brown become Quick-Brown.
  3. Capitalize Words: Convert the first letter of all other words to uppercase and the remaining letters to lowercase. Words without hyphens can be transformed directly using the capitalize() method.
  4. Join and Reconstruct: After transforming each part of the text, join the words back together while maintaining the original spaces.
  5. Apply Transformation: Use a function that applies these steps to each entry in content_text, creating a new column converted_text in the DataFrame.

Through this systematic approach, the problem can be effectively solved, ensuring that both hyphenated and non-hyphenated words are correctly capitalized in accordance with the rules provided.

Solution Approach

The implementation of the solution involves using the pandas library to manipulate the data within the DataFrame. Here's a detailed walkthrough of the solution:

  1. Import Pandas: We start by importing the pandas library, which provides data structures and functions for handling DataFrames efficiently.

    import pandas as pd
  2. Define the Function: We define a function named capitalize_content that processes the DataFrame.

    def capitalize_content(user_content: pd.DataFrame) -> pd.DataFrame:
  3. Helper Function Define convert_text: Inside the capitalize_content function, we define a helper function convert_text that will handle the text transformation. This function takes a string text as input and returns a transformed string.

    def convert_text(text: str) -> str:
    • Process Each Word: We use a nested list comprehension to process each word in text. The outer comprehension iterates over each word split by spaces, while the inner comprehension splits any hyphenated words and capitalizes each part.

      return " ".join(
          (
              "-".join([part.capitalize() for part in word.split("-")])
              if "-" in word
              else word.capitalize()
          )
          for word in text.split(" ")
      )
    • Hyphen Handling: If a word contains a hyphen, it is split on the hyphen, and each part is capitalized before being joined back together.

  4. Apply Text Transformation: We use the apply() method of pandas to apply convert_text to the content_text column, thus creating a new column converted_text.

    user_content["converted_text"] = user_content["content_text"].apply(convert_text)
  5. Rename Column for Output: We rename the original content_text column to original_text for clarity in the output DataFrame.

    return user_content.rename(columns={"content_text": "original_text"})[
        ["content_id", "original_text", "converted_text"]
    ]

Through this approach, the solution efficiently transforms the text while maintaining the structure and format of the original content. The use of list comprehensions in tandem with pandas methods ensures that the data is processed quickly and accurately.

Ready to land your dream job?

Unlock your dream job with a 2-minute evaluator for a personalized learning plan!

Start Evaluator

Example Walkthrough

Let's walk through a simple example to see how the solution approach transforms the text in the user_content table.

Example Data:

Suppose the user_content table contains the following data:

content_idcontent_text
1"hello world"
2"pandas-is awesome"
3"multi-part title"

Transformation Process:

  1. Split the Text:

    • For content_id 1: "hello world" splits into words: ["hello", "world"].
    • For content_id 2: "pandas-is awesome" splits into words: ["pandas-is", "awesome"].
    • For content_id 3: "multi-part title" splits into words: ["multi-part", "title"].
  2. Handle Hyphenated Words:

    • For content_id 2: Split "pandas-is" gives ["pandas", "is"]. Each piece is capitalized to form "Pandas-Is".
    • For content_id 3: Split "multi-part" results in ["multi", "part"]. Each piece is modified to form "Multi-Part".
  3. Capitalize Words:

    • For content_id 1: "hello" becomes "Hello" and "world" becomes "World".
    • For content_id 2: "awesome" becomes "Awesome".
    • For content_id 3: "title" is transformed into "Title".
  4. Join and Reconstruct:

    • For content_id 1: Words are joined to form "Hello World".
    • For content_id 2: Hyphenated and single words create "Pandas-Is Awesome".
    • For content_id 3: The combination results in "Multi-Part Title".
  5. Apply Transformation to DataFrame:

    • The modified texts are set in the new converted_text column.

Final Result:

content_idoriginal_textconverted_text
1hello worldHello World
2pandas-is awesomePandas-Is Awesome
3multi-part titleMulti-Part Title

This output shows each original text alongside its transformed version, demonstrating the conversion rules in action.

Solution Implementation

1import pandas as pd
2
3def capitalize_content(user_content: pd.DataFrame) -> pd.DataFrame:
4    """
5    Capitalizes the content of the provided DataFrame and returns a new DataFrame
6    with the original and converted text.
7
8    Args:
9    user_content (pd.DataFrame): DataFrame containing 'content_id' and 'content_text'.
10
11    Returns:
12    pd.DataFrame: A DataFrame with 'content_id', 'original_text', and 'converted_text'.
13    """
14    def convert_text(text: str) -> str:
15        """Capitalizes each word in the provided text."""
16        return " ".join(
17            (
18                # Capitalizes each part of hyphenated words
19                "-".join([part.capitalize() for part in word.split("-")])
20                if "-" in word
21                else word.capitalize()
22            )
23            for word in text.split(" ")  # Splits the text into individual words
24        )
25
26    # Apply the convert_text function to the 'content_text' column
27    user_content["converted_text"] = user_content["content_text"].apply(convert_text)
28  
29    # Rename the 'content_text' column to 'original_text'
30    return user_content.rename(columns={"content_text": "original_text"})[
31        ["content_id", "original_text", "converted_text"]  # Select and order the columns
32    ]
33
1import java.util.ArrayList;
2import java.util.List;
3import java.util.stream.Collectors;
4
5public class CapitalizeContent {
6
7    // Class representing the data structure which contains content ID and text
8    public static class Content {
9        private int contentId;
10        private String originalText;
11        private String convertedText;
12
13        public Content(int contentId, String originalText, String convertedText) {
14            this.contentId = contentId;
15            this.originalText = originalText;
16            this.convertedText = convertedText;
17        }
18
19        public int getContentId() {
20            return contentId;
21        }
22
23        public String getOriginalText() {
24            return originalText;
25        }
26
27        public String getConvertedText() {
28            return convertedText;
29        }
30    }
31
32    // Method to capitalize each word in a given string
33    private static String convertText(String text) {
34        // Splitting the text into words, capitalizing each word, and rejoining them
35        return java.util.Arrays.stream(text.split(" "))
36                .map(word -> {
37                    // Checking for hyphenated words
38                    if (word.contains("-")) {
39                        return java.util.Arrays.stream(word.split("-"))
40                                .map(part -> part.substring(0, 1).toUpperCase() + part.substring(1).toLowerCase())
41                                .collect(Collectors.joining("-"));
42                    } else {
43                        return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase();
44                    }
45                })
46                .collect(Collectors.joining(" "));
47    }
48
49    // Main method to handle input and output of contents
50    public static List<Content> capitalizeContent(List<Content> userContent) {
51        List<Content> newContentList = new ArrayList<>();
52
53        // Iterating through content list
54        for (Content content : userContent) {
55            String originalText = content.getOriginalText();
56            String convertedText = convertText(originalText);  // Capitalizing text
57            Content newContent = new Content(content.getContentId(), originalText, convertedText);  // Creating new content object
58            newContentList.add(newContent);  // Adding to the new list
59        }
60
61        return newContentList;
62    }
63
64    // Main program execution for testing
65    public static void main(String[] args) {
66        List<Content> data = List.of(
67                new Content(1, "hello world", ""),
68                new Content(2, "java-programming", ""),
69                new Content(3, "java and pandas", "")
70        );
71
72        List<Content> result = capitalizeContent(data);
73
74        // Output the results
75        for (Content content : result) {
76            System.out.println("Content ID: " + content.getContentId());
77            System.out.println("Original Text: " + content.getOriginalText());
78            System.out.println("Converted Text: " + content.getConvertedText());
79        }
80    }
81}
82
1#include <iostream>
2#include <string>
3#include <vector>
4#include <sstream>
5#include <algorithm>
6#include <cctype>
7
8// This function capitalizes each word in the provided text.
9std::string convertText(const std::string& text) {
10    std::istringstream stream(text);
11    std::string word;
12    std::string result;
13  
14    while (stream >> word) {
15        for (size_t i = 0; i < word.size(); ++i) {
16            // Capitalize the first letter of each part of hyphenated words
17            if (i == 0 || word[i - 1] == '-') {
18                word[i] = std::toupper(word[i]);
19            } else {
20                word[i] = std::tolower(word[i]);
21            }
22        }
23        if (!result.empty()) {
24            result += " ";
25        }
26        result += word;
27    }
28  
29    return result;
30}
31
32// Define a struct to store content information
33struct Content {
34    int contentId;
35    std::string originalText;
36    std::string convertedText;
37};
38
39// This function capitalizes the content of the provided vector of Content
40std::vector<Content> capitalizeContent(const std::vector<Content>& userContent) {
41    std::vector<Content> result;
42    for (const auto& content : userContent) {
43        Content newContent;
44        newContent.contentId = content.contentId;
45        newContent.originalText = content.originalText;
46        newContent.convertedText = convertText(content.originalText);
47        result.push_back(newContent);
48    }
49    return result;
50}
51
52// Optional: Function to print the content for demonstration purposes
53void printContent(const std::vector<Content>& contentList) {
54    for (const auto& content : contentList) {
55        std::cout << "ID: " << content.contentId
56                  << ", Original: " << content.originalText
57                  << ", Converted: " << content.convertedText << std::endl;
58    }
59}
60
61int main() {
62    // Sample data
63    std::vector<Content> data = {
64        {1, "hello world"},
65        {2, "sample-text to convert"},
66        {3, "another example"}
67    };
68
69    // Capitalize content
70    std::vector<Content> capitalizedData = capitalizeContent(data);
71
72    // Print results
73    printContent(capitalizedData);
74
75    return 0;
76}
77
1import * as pd from 'pandas-js';
2
3// Function to capitalize the content of a DataFrame
4function capitalize_content(user_content: pd.DataFrame): pd.DataFrame {
5    /**
6     * Capitalizes the content of the provided DataFrame and returns a new DataFrame
7     * with the original and converted text.
8     *
9     * @param user_content - DataFrame containing 'content_id' and 'content_text'.
10     * @returns DataFrame with 'content_id', 'original_text', and 'converted_text'.
11     */
12  
13    // Helper function to capitalize each word in a text string
14    function convert_text(text: string): string {
15        return text.split(" ").map(word => {
16            // Capitalize each part of hyphenated words
17            return word.includes("-") 
18                ? word.split("-").map(part => capitalize(part)).join("-")
19                : capitalize(word);
20        }).join(" ");
21    }
22
23    // Helper function to capitalize a single word
24    function capitalize(word: string): string {
25        return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
26    }
27
28    // Apply the convert_text function to the 'content_text' column
29    user_content['converted_text'] = user_content['content_text'].apply(convert_text);
30  
31    // Rename the 'content_text' column to 'original_text'
32    user_content = user_content.rename({ 'content_text': 'original_text' });
33
34    // Select and order the columns
35    return user_content.select(['content_id', 'original_text', 'converted_text']);
36}
37

Time and Space Complexity

The time complexity of the code is O(n * m * k), where n is the number of rows in the DataFrame, m is the average number of words per content_text, and k is the average length of each word. This complexity results from iterating over each row in the DataFrame and processing each word in the content_text strings.

The space complexity is O(n * m * k), primarily due to the storage of the converted strings in the new column converted_text. This involves an additional DataFrame column that scales with the input size.

Learn more about how to find time and space complexity quickly.


Discover Your Strengths and Weaknesses: Take Our 2-Minute Quiz to Tailor Your Study Plan:
Question 1 out of 10

Which type of traversal does breadth first search do?


Recommended Readings

Want a Structured Path to Master System Design Too? Don’t Miss This!


Load More