Skip to content

Practice First Letter Capitalization II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3374
LeetCode Problem

First Letter Capitalization II

Problem Statement

Table user_content has content_id INT and content_text VARCHAR. A word is a nonempty run between spaces. Leave a word unchanged if its first character is not an English letter. For a word made entirely of two or more letter-only parts joined by single hyphens, capitalize each part and lowercase its remaining letters. Otherwise uppercase the first letter and lowercase subsequent English letters while preserving punctuation. Preserve every space. Return content_id, original_text, and converted_text; no output order is specified. Practice approach: Separate spaces from words without collapsing them. Classify each whole word before transforming it: a broad title-case function can incorrectly capitalize punctuation-separated pieces. Test repeated spaces, a nonletter first character, a clean hyphen chain, and a word with repeated hyphens.

Source: First Letter Capitalization II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
{
  "user_content": [
    {
      "content_id": 1,
      "content_text": "hELLO  @KEEP fr-ONT-END foo--BAR"
    },
    {
      "content_id": 2,
      "content_text": "a/B-C -KEEP x^Y"
    }
  ]
}
Output
[
  {
    "content_id": 1,
    "original_text": "hELLO  @KEEP fr-ONT-END foo--BAR",
    "converted_text": "Hello  @KEEP Fr-Ont-End Foo--bar"
  },
  {
    "content_id": 2,
    "original_text": "a/B-C -KEEP x^Y",
    "converted_text": "A/b-c -KEEP X^y"
  }
]
Explanation

Nonletter-leading words remain unchanged. Double spaces survive, and punctuation inside an otherwise ordinary word does not start a new capitalized part.

Constraints

content_id is unique.
Input characters are English letters, spaces, backslash, @, -, /, ^, and comma.

Related LeetCode Problems

Practice more problems similar to First Letter Capitalization II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.