Problem overview
The method concatFirstAndLastChar receives a string s with at least two characters and returns a new two-character string: the first character of s followed by its last character. Every character counts, spaces and punctuation included, and letters keep their case.
For s = "hello" the first character is h and the last is o, so the result is "ho". For "Hello World!" the last character is the exclamation mark, so the result is "H!". For "ab" the two characters are the whole string, and the result is "ab".
Approach
You need two single characters and the + operator to join them. Indexing gives you each character. The first one is at index 0. The last one depends on the length of the string, but Python lets you count from the end with a negative index: -1 is the last character, -2 the one before it, and so on. That avoids computing len(s) - 1 yourself.
Step by step
- Take the first character with
s[0]. - Take the last character with
s[-1]. - Join them with
+. Adding two strings builds a new string, so the originalsis untouched. - Return the result.
The solution in Python
class Solution:
def concatFirstAndLastChar(self, s: str) -> str:
return s[0] + s[-1]
Some details worth knowing:
- Indexing a string gives another string of length 1. Python has no separate character type, so
s[0] + s[-1]is plain string concatenation. - With a negative index,
s[-1]meanss[len(s) - 1]. It works for any length, so the same code handles"ab"and a long sentence. - Strings are immutable. Nothing about
schanges; the function builds and returns a new string.
How positive and negative indexes line up
Every character of a string has two valid indexes: one counted from the front, starting at 0, and one counted from the back, starting at -1. For "hello":
| character | h | e | l | l | o |
|---|---|---|---|---|---|
| from the front | 0 | 1 | 2 | 3 | 4 |
| from the back | -5 | -4 | -3 | -2 | -1 |
Both rows pick the same characters. In "Hello World!", which has 12 characters, s[0] is H and both s[-1] and s[11] are !. The negative form is shorter and doesn't depend on the length, so it is the usual choice for "the last one".
The same idea with named variables
When a one-line expression feels dense, split it into steps. This version behaves exactly like the solution above, and for "Hello World!" it prints H and ! on the first line, then H!:
s = "Hello World!"
first = s[0]
last = s[-1]
print(first, last)
print(first + last)
Naming the pieces makes each step readable and easy to print while you debug. The one-line form is the same thing without the temporary names. Use whichever you can read at a glance, and test both against the examples.
Edge cases and common mistakes
- A two-character string.
"ab"gives"ab": index0and index-1are different positions, so each character is used once. The constraints guarantee at least two characters. - Too short a string. The lesson never passes one, but an empty string would fail:
""[0]raisesIndexError: string index out of range. - Using
s[len(s)]for the last character. Valid indexes stop atlen(s) - 1, so this raises the sameIndexError. Use-1. - Trailing punctuation. The last character of
"Hello World!"is!, notd. Don't skip punctuation or spaces.
Testing
"hello"gives"ho"."Hello World!"gives"H!"."ab"gives"ab"."xyz"gives"xz".
