Skip to content

Commit 7a244e9

Browse files
solve #2129: Capitalize the Title in java
1 parent cabc319 commit 7a244e9

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@
704704
| 2114 | [Maximum Number of Words Found in Sentences](https://leetcode.com/problems/maximum-number-of-words-found-in-sentences) | [![Java](assets/java.png)](src/MaximumNumberOfWordsFoundInSentences.java) | |
705705
| 2119 | [A Number After a Double Reversal](https://leetcode.com/problems/a-number-after-a-double-reversal) | [![Java](assets/java.png)](src/ANumberAfterADoubleReversal.java) | |
706706
| 2124 | [Check if All A's Appears Before All B's](https://leetcode.com/problems/check-if-all-as-appears-before-all-bs) | [![Java](assets/java.png)](src/CheckIfAllTheAsAppearBeforeAllTheBs.java) | |
707-
| 2129 | [Capitalize the Title](https://leetcode.com/problems/capitalize-the-title) | | |
707+
| 2129 | [Capitalize the Title](https://leetcode.com/problems/capitalize-the-title) | [![Java](assets/java.png)](src/CapitalizeTheTitle.java) | |
708708
| 2133 | [Check if Every Row and Column Contains All Numbers](https://leetcode.com/problems/check-if-every-row-and-column-contains-all-numbers) | | |
709709
| 2138 | [Divide a String Into Groups of Size k](https://leetcode.com/problems/divide-a-string-into-groups-of-size-k) | | |
710710
| 2144 | [Minimum Cost of Buying Candies With Discount](https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount) | | |

Diff for: src/CapitalizeTheTitle.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// https://leetcode.com/problems/capitalize-the-title
2+
// T: O(|s|)
3+
// S: O(|s|)
4+
5+
public class CapitalizeTheTitle {
6+
public String capitalizeTitle(String title) {
7+
final StringBuilder result = new StringBuilder();
8+
final String[] words = title.split(" ");
9+
for (String word : words) {
10+
if (word.length() <= 2) result.append(word.toLowerCase()).append(' ');
11+
else result.append(word.substring(0, 1).toUpperCase()).append(word.substring(1).toLowerCase()).append(' ');
12+
}
13+
return result.delete(result.length() - 1, result.length()).toString();
14+
}
15+
}

0 commit comments

Comments
 (0)