Problem overview
The function concatFirstAndLastChar receives a string s with at least two characters. It returns a new string of exactly two characters: the first character of s, followed by its last one.
For "hello" the answer is "ho". Every character counts, so for "Hello World!" the last character is the exclamation mark and the result is "H!". The case of a letter never changes. A two-character string such as "ab" comes back unchanged, because its first and last characters are the whole string.
Approach
The task has two small parts: pick out the first character, pick out the last one, and glue them together. In C++ the natural tool for picking out a piece of a string is substr. It returns a new std::string and leaves the original alone.
The first character sits at index 0. The last one sits at index s.size() - 1, because indexes start at 0 and a string of length 5 has indexes 0 to 4. Taking one character from each place gives two one-character strings, and + joins two strings into one.
Step by step
- Take the first character as a string.
substr(pos, count)starts atposand takes at mostcountcharacters, sos.substr(0, 1)is a string holding just the first character. - Take the last character the same way. The call
s.substr(s.size() - 1)starts at the last index. With no second argument,substrruns to the end of the string, and from the last index that is exactly one character. - Join the two pieces with
+and return the result.
The solution in C++
class Solution {
public:
string concatFirstAndLastChar(string s) {
return s.substr(0, 1) + s.substr(s.size() - 1);
}
};
A few C++ details explain why the code is written this way:
- Why
substrand nots[0]. The expressions[0]is a singlechar, which is a small integer type. Adding twocharvalues adds their character codes as numbers:'h' + 'o'is theint215, not the text"ho". Twostd::stringvalues, on the other hand, join as text. - The second argument is a count. In
substr(0, 1)the 1 means "take one character". It is not the index where the piece stops. - The result is a new string. The parameter
sis passed by value, so the function works on its own copy, and the caller's string is untouched either way. size()is unsigned.s.size()has an unsigned type, so on an empty strings.size() - 1wraps around to a huge number. Here the constraints guarantee at least two characters, so this doesn't happen.
Edge cases and common mistakes
- Using
+on twocharvalues. Writings[0] + s[s.size() - 1]adds two numbers and gives anint, which the function can't return as astring: the code doesn't compile. Usesubstrto get strings. - Off by one on the last index.
s.substr(s.size())is legal and returns an empty string, so the result would be just the first character. The last character starts ats.size() - 1. - A string of exactly two characters.
"ab"gives"a"plus"b", which is"ab". The two pieces never overlap, and the code needs no special case. - An empty string. Calling
substrwith a start position beyond the end of the string throwsstd::out_of_range. On an empty string,s.size() - 1wraps to a huge number and the secondsubstrcall throws. The lesson's inputs never have fewer than two characters.
Testing
"hello"gives"ho"."Hello World!"gives"H!"."ab"gives"ab".
