Problem overview
The method 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 upper-case H stays as it is. For "ab" the first and last characters are the whole string, so the answer is "ab".
Approach
A string's characters are numbered from 0, so the first character is at index 0 and the last one is at index s.length - 1. The task is to cut out those two one-character pieces and glue them together.
Scala offers two ways to read one character. s.charAt(i) gives a Char, and s.substring(i, j) gives a String holding the characters from i up to, but not including, j. The difference matters here. The + between two Char values adds their character codes as numbers, while + between two strings joins them as text. With s = "hello", the expression s.charAt(0) + s.charAt(s.length - 1) gives the number 215 (104 plus 111), not "ho". Slices keep the pieces as strings, so the join works as expected.
Step by step
- Take the first character as a slice with
s.substring(0, 1). The end index is excluded, so this is exactly the character at index 0. - Take the last character as a slice with
s.substring(s.length - 1). With one argument,substringstarts there and runs to the end of the string, which leaves just the last character. - Join the two strings with
+and return the result.
The solution in Scala
class Solution {
def concatFirstAndLastChar(s: String): String = {
return s.substring(0, 1) + s.substring(s.length - 1)
}
}
A few Scala details:
returnis allowed, and the lesson uses it. In Scala the last expression of a block is its value anyway, so the same method also works without the wordreturn.substringis a Java method, available on every ScalaString. Its end index is exclusive, which is whysubstring(0, 1)keeps one character.- Scala has other ways to get a character, such as
s.headands.last. They return aChar, so they need.toStringbefore they are joined with+as text.
Tracing an example
Take s = "Hello World!". Its length is 12, so the last index is 11. The first slice, s.substring(0, 1), is "H". The second slice starts at s.length - 1, which is 11, and runs to the end: it is "!". Joined, they make "H!". The space in the middle and every letter between the two ends are never touched, because the method only looks at the two ends.
For "hello" the length is 5, so the second slice starts at index 4 and gives "o". The first slice gives "h", and the answer is "ho".
Edge cases and common mistakes
- Joining Chars with
+. As shown above, twoCharvalues add up as numbers. Use slices, or convert eachCharto a string first. - Using
s.lengthas the index. The last valid index iss.length - 1. Starting a slice ats.lengthgives an empty string, and starting beyond it throws an exception. - Two-character strings. For
"ab"the two slices are"a"and"b", so the method returns the same string. Nothing special is needed. - Trimming or changing case. The method returns the characters exactly as they are, punctuation and capital letters included.
Testing
"hello"gives"ho"."Hello World!"gives"H!"."ab"gives"ab".
