Capitalize the Title

🏠 ⬅️ ➡️

You are given a string title consisting of one or more words separated by a single space, where each word consists of English letters. Capitalize the string by changing the capitalization of each word such that:

  • If the length of the word is 1 or 2 letters, change all letters to lowercase.
  • Otherwise, change the first letter to uppercase and the remaining letters to lowercase.

Return the capitalized title.

Example 1:

Input: title = "capiTalIze tHe titLe " Output: "Capitalize The Title " Explanation: Since all the words have a length of at least 3, the first letter of each word is uppercase, and the remaining letters are lowercase.

Example 2:

Input: title = "First leTTeR of EACH Word " Output: "First Letter of Each Word " Explanation: The word "of " has length 2, so it is all lowercase. The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.

Example 3:

Input: title = "i lOve leetcode " Output: "i Love Leetcode " Explanation: The word "i " has length 1, so it is lowercase. The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.

Constraints:

  • 1 <= title.length <= 100
  • title consists of words separated by a single space without any leading or trailing spaces.
  • Each word consists of uppercase and lowercase English letters and is non-empty.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    character(len=100) :: title

    title = "capiTalIze tHe titLe "
    print '(A)', capitalize(title)

    title = "First leTTeR of EACH Word "
    print '(A)', capitalize(title)

    title = "i lOve leetcode "
    print '(A)', capitalize(title)

contains

    function capitalize(str) result(res)
        implicit none
        character(len=*), intent(in) :: str
        character(len=len(str)) :: res
        integer :: i, n

        n = len(str)
        do i = 1, n
            if (i == 1 .or. i == 2) then
                res(i:i) = tolower(str(i:i))
            else
                res(i:i) = toupper(str(i:i))
            end if
        end do
    end function capitalize

end program main
Compiled
Executed
Correct
PROGRAM CAPITALIZE_TITLE
      IMPLICIT NONE

      CHARACTER(len=100) :: title
      CHARACTER(len=100) :: capitalized_title
      INTEGER :: i, j, n

      ! Read the input title
      READ(*,*) title

      ! Initialize the capitalized title
      capitalized_title = ""

      ! Loop through each word in the title
      n = 1
      DO i = 1, LEN_TRIM(title)
        ! Check if the current character is a space
        IF (title(i:i) == " ") THEN
          ! If the current character is a space, add it to the capitalized title
          capitalized_title(n:n) = " "
          n = n + 1
        ELSE
          ! If the current character is not a space, check its length
          DO j = i, LEN_TRIM(title)
            ! If the current character is a letter, check its length
            IF (title(j:j) == " ") EXIT
          END DO

          ! If the current character is a letter with length 1 or 2, change it to lowercase
          IF (j - i + 1 <= 2) THEN
            capitalized_title(n:n) = LOWER(title(i:j))
          ! Otherwise, change the first letter to uppercase and the remaining letters to lowercase
          ELSE
            capitalized_title(n:n) = UPPER(title(i:i))
            capitalized_title(n+1:n+j-i) = LOWER(title(i+1:j))
          END IF

          ! Increment the index for the capitalized title
          n = n + j - i + 1
        END IF
      END DO

      ! Print the capitalized title
      WRITE(*,*) capitalized_title

      END PROGRAM CAPITALIZE_TITLE
🌐 Data from online sources
def interchangeable_rectangles(rectangles):
    ratio_count = {}
    count = 0

    for r in rectangles:
        ratio = r[0] / r[1]
        count += ratio_count.get(ratio, 0)
        ratio_count[ratio] = ratio_count.get(ratio, 0) + 1

    return count
For each rectangle, we calculate the width-to-height ratio by dividing the width by the height as a floating-point number. We use a hash map called `ratioCount` to maintain the counts of each unique ratio found so far. If the current ratio is already in the hash map, we increment the count of the current ratio by 1 and add the current count of the ratio to our result. In this way, we count the pairs of interchangeable rectangles that can be created with the current rectangle for each ratio.

For example, if the input array is rectangles = [[4, 8], [3, 6], [10, 20], [12, 24], [12, 16]], then we calculate the following width-to-height ratios: [1/2, 1/2, 1/2, 1/2, 3/4]. The count of the ratio 1/2 is 4, so the result will be 1 + 2 + 3 = 6, as these are the number of pairs that can be formed with the 4 rectangles with this ratio.

🌐 Data from online sources
#include <unordered_map>
#include <vector>

int interchangeableRectangles(std::vector<std::vector<int>>& rectangles) {
    std::unordered_map<double, int> ratioCount;
    int count = 0;

    for (const auto& r : rectangles) {
        double ratio = static_cast<double>(r[0]) / r[1];
        count += ratioCount[ratio];
        ratioCount[ratio]++;
    }

    return count;
}
For each rectangle, we calculate the width-to-height ratio by dividing the width by the height as a floating-point number. We use a hash map called `ratioCount` to maintain the counts of each unique ratio found so far. If the current ratio is already in the hash map, we increment the count of the current ratio by 1 and add the current count of the ratio to our result. In this way, we count the pairs of interchangeable rectangles that can be created with the current rectangle for each ratio.

For example, if the input array is rectangles = [[4, 8], [3, 6], [10, 20], [12, 24], [12, 16]], then we calculate the following width-to-height ratios: [1/2, 1/2, 1/2, 1/2, 3/4]. The count of the ratio 1/2 is 4, so the result will be 1 + 2 + 3 = 6, as these are the number of pairs that can be formed with the 4 rectangles with this ratio.