Problem overview
The function concatFirstAndLastChar receives a string s of at least two characters and returns a new two-character string: the first character of s followed by its last character.
For "hello" the answer is "ho". For "Hello World!" it is "H!": every character counts, so the last one is the exclamation mark, and the capital H stays a capital. For "ab" the first and last characters are the whole string, so the answer is "ab".
Approach
A Kotlin string can be indexed with square brackets, and counting starts at 0. The first character is s[0]. The last one sits at index s.length - 1, because a string of length 5 has indexes 0 to 4.
The only question is how to join the two characters. Indexing gives a Char, not a String, and Char has no + that takes another Char. Writing s[0] + s[s.length - 1] doesn't compile: the compiler reports an argument type mismatch, because the only + on a Char expects an Int.
A string template solves it. Inside a string literal, ${...} evaluates the expression and inserts its text, so two templates side by side build a two-character string.
Step by step
- Start a string literal with
". - Insert the first character with
${s[0]}. - Insert the last character with
${s[s.length - 1]}. - Close the literal and return it.
The solution in Kotlin
class Solution {
fun concatFirstAndLastChar(s: String): String {
return "${s[0]}${s[s.length - 1]}"
}
}
A few details about this code:
Chararithmetic is not text joining. Withval c = 'h', the expressionc + 1is the next character,'i': adding anIntto aCharmoves along the alphabet. That is whyChar + Charis rejected instead of being treated as concatenation.- Other ways to join.
s[0].toString() + s[s.length - 1]works too, becauseString + Charis allowed and gives aString. So does"" + s[0] + s[s.length - 1]. For"hello"both give"ho". first()andlast(). Kotlin also offerss.first()ands.last(), which return the same two characters without index arithmetic.- Characters, not bytes. Indexing counts UTF-16 units, which is the same as characters for the English letters, digits and punctuation this lesson uses.
Tracing the examples
For "hello", s.length is 5, so the last index is 4. s[0] is 'h' and s[4] is 'o'. The two templates each insert one character, and the result is "ho", a string of length 2.
For "Hello World!", s.length is 12 and s[11] is '!'. The space in the middle and the letters around it play no part: only the two ends are read, and the answer is "H!".
The same two lines of reasoning cover every input, whatever the length. That is why one expression is enough and no loop or condition is needed.
Edge cases and common mistakes
- Using
s.lengthas the last index. The valid indexes stop ats.length - 1. Indexs.lengththrows aStringIndexOutOfBoundsException. - Adding the two characters.
s[0] + s[s.length - 1]fails to compile, as described above. If it did compile, adding character codes would give a number, not a string:'h'.code + 'o'.codeis215. - Empty strings. The lesson promises at least two characters. On an empty string,
s[0]throwsStringIndexOutOfBoundsExceptiontoo. - Changing the case. Nothing here converts letters. The
Hin"Hello World!"stays upper case.
Testing
"hello"gives"ho"."Hello World!"gives"H!"."ab"gives"ab".
