|
| 1 | +/** |
| 2 | + * 2129 |
| 3 | + * Capitalize the Title |
| 4 | + ** |
| 5 | + * You are given a string title |
| 6 | + * consisting of one or more words separated by a single space, |
| 7 | + * where each word consists of English letters. |
| 8 | + * Capitalize the string by changing the capitalization of each word such that: |
| 9 | + * • If the length of the word is 1 or 2 letters, change all letters to lowercase. |
| 10 | + * • Otherwise, change the first letter to uppercase and the remaining letters to lowercase. |
| 11 | + * |
| 12 | + * Return the capitalized title. |
| 13 | + * |
| 14 | + * Example 1: |
| 15 | + * Input: title = "capiTalIze tHe titLe" |
| 16 | + * Output: "Capitalize The Title" |
| 17 | + * Explanation: |
| 18 | + * Since all the words have a length of at least 3, |
| 19 | + * the first letter of each word is uppercase, |
| 20 | + * and the remaining letters are lowercase. |
| 21 | + * |
| 22 | + * Example 2: |
| 23 | + * Input: title = "First leTTeR of EACH Word" |
| 24 | + * Output: "First Letter of Each Word" |
| 25 | + * Explanation: |
| 26 | + * The word "of" has length 2, so it is all lowercase. |
| 27 | + * The remaining words have a length of at least 3, |
| 28 | + * so the first letter of each remaining word is uppercase, |
| 29 | + * and the remaining letters are lowercase. |
| 30 | + * |
| 31 | + * Example 3: |
| 32 | + * Input: title = "i lOve leetcode" |
| 33 | + * Output: "i Love Leetcode" |
| 34 | + * Explanation: |
| 35 | + * The word "i" has length 1, so it is lowercase. |
| 36 | + * The remaining words have a length of at least 3, |
| 37 | + * so the first letter of each remaining word is uppercase, |
| 38 | + * and the remaining letters are lowercase. |
| 39 | + * |
| 40 | + * Constraints: |
| 41 | + * • 1 <= title.length <= 100 |
| 42 | + * • title consists of words separated by a single space without any leading or trailing spaces. |
| 43 | + * • Each word consists of uppercase and lowercase English letters and is non-empty. |
| 44 | + * |
| 45 | + * Hint 1: |
| 46 | + * Firstly, try to find all the words present in the string. |
| 47 | + * |
| 48 | + * Hint 2: |
| 49 | + * On the basis of each word's lengths, |
| 50 | + * simulate the process explained in Problem. |
| 51 | + ** |
| 52 | + * https://leetcode.com/problems/capitalize-the-title/ |
| 53 | +***/ |
| 54 | + |
| 55 | +using System; |
| 56 | + |
| 57 | +namespace Problems; |
| 58 | + |
| 59 | +public class CapitalizeTheTitle |
| 60 | +{ |
| 61 | + public string CapitalizeTitle( string title ) |
| 62 | + { |
| 63 | + char[] result = new char[title.Length]; |
| 64 | + int left = 0; |
| 65 | + |
| 66 | + for ( int right = 0; right < result.Length; right++ ) |
| 67 | + { |
| 68 | + if ( title[right] == ' ' ) |
| 69 | + { |
| 70 | + result[right] = title[right]; |
| 71 | + |
| 72 | + if ( right - left > 2 ) |
| 73 | + { |
| 74 | + result[left] = Char.ToUpper( title[left] ); |
| 75 | + } |
| 76 | + |
| 77 | + left = right + 1; |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + result[right] = Char.ToLower( title[right] ); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if ( result.Length - left > 2 ) |
| 86 | + { |
| 87 | + result[left] = Char.ToUpper( title[left] ); |
| 88 | + } |
| 89 | + |
| 90 | + return new String( result ); |
| 91 | + } |
| 92 | +} |
0 commit comments