Problem overview
The function initials receives a full name made of a first name and a last name, separated by one space. It returns the first letter of each name, in upper case.
For "grace hopper" the answer is "GH". For "Ada Lovelace" it is "AL", since the letters are already capitals. For "a b" it is "AB": a name can be one letter long.
Approach
There are three small jobs: separate the two names, take the first letter of each, and convert the result to upper case. split(" ") separates the names into a list. substring(0, 1) takes the first letter of a name as a String. uppercase() converts a string to upper case.
Step by step
- Split the full name at the space into a list named
words, declared withval. With exactly one space, there are exactly two words. - Take the first letter of each word with
substring(0, 1), add the two letters with+, put the sum in parentheses and calluppercase()on it. The parentheses make the conversion apply to both letters. Without them, it would apply only to the second one.
The solution in Kotlin
class Solution {
fun initials(fullName: String): String {
val words = fullName.split(" ")
return (words[0].substring(0, 1) + words[1].substring(0, 1)).uppercase()
}
}
Details worth knowing:
- Why
substring(0, 1). Indexing, as inwords[0][0], gives aChar, and twoCharvalues can't be added into a string. A one-letter slice is aString, so+concatenates the letters. - Two kinds of brackets.
words[0]reads an item of the list, andsubstringis called on that item. Reading left to right: the first word, then its first letter. uppercase()returns a new string. The original is unchanged. In Kotlin 1.5 and later this is the function to use, and it replaces the oldertoUpperCase().- Letters that are already capitals stay capitals. Converting
"AL"to upper case gives"AL", which is why one call handles both"grace hopper"and"Ada Lovelace".
Tracing the examples
For "grace hopper", the split gives [grace, hopper]. The first letters are "g" and "h", their sum is "gh", and uppercase() turns it into "GH".
For "Ada Lovelace", the letters are "A" and "L", and the result is "AL".
For "a b", each word has one letter, so substring(0, 1) is the whole word. The result is "AB".
Another way to write it
The same result can come from a transformation of the list: fullName.split(" ").map { it.substring(0, 1) }.joinToString("").uppercase() takes the first letter of every word, glues them with an empty separator and converts the text. For "grace hopper" it gives "GH". It would also work for three names, while the reference solution is written for exactly two. For a two-name exercise, the reference solution is shorter to read and easier to trace.
Edge cases and common mistakes
- Converting only one letter.
words[0].substring(0, 1) + words[1].substring(0, 1).uppercase()converts just the second letter, because the call binds to the nearest piece. For"grace hopper"it would give"gH". - A single word.
"grace".split(" ")gives a list with one item, sowords[1]throws anIndexOutOfBoundsException. The lesson guarantees two words. - Extra spaces. Two spaces in a row would produce an empty word in the middle. The lesson promises a single space.
- Special cases of upper-casing. Some letters change length when converted:
"ß".uppercase()is"SS". The lesson uses English letters only, so every initial stays one letter.
Testing
"grace hopper"gives"GH"."Ada Lovelace"gives"AL"."a b"gives"AB".
