|
| 1 | +class Solution { |
| 2 | + public String generateTag(String caption) { |
| 3 | + StringBuilder sb = new StringBuilder(); |
| 4 | + sb.append('#'); |
| 5 | + int idx = 0; |
| 6 | + int n = caption.length(); |
| 7 | + while (idx < n && caption.charAt(idx) == ' ') { |
| 8 | + idx++; |
| 9 | + } |
| 10 | + boolean upperCase = false; |
| 11 | + while (idx < n) { |
| 12 | + if (Character.isLetter(caption.charAt(idx))) { |
| 13 | + if (upperCase) { |
| 14 | + sb.append(Character.toUpperCase(caption.charAt(idx))); |
| 15 | + upperCase = false; |
| 16 | + } else { |
| 17 | + sb.append(Character.toLowerCase(caption.charAt(idx))); |
| 18 | + } |
| 19 | + } else if (caption.charAt(idx) == ' ') { |
| 20 | + upperCase = true; |
| 21 | + } |
| 22 | + idx++; |
| 23 | + } |
| 24 | + return sb.toString().substring(0, Math.min(sb.length(), 100)); |
| 25 | + } |
| 26 | +} |
0 commit comments