Problem overview
A website builds a user ID from a person's name: the first 2 letters of the first name followed by the last 3 letters of the last name. The function creatingIds receives first and last and returns that ID.
For "John" and "Smith" the first two letters are Jo and the last three letters of the surname are ith, so the ID is "Joith". For "Jo" and "Doe" the names have exactly the needed lengths, so both are used whole and the ID is "JoDoe". Letters keep their case: "ada" and "Lovelace" give "adace".
Approach: take pieces from the front and from the back
The problem needs two pieces of text, one counted from the start of a string and one counted from its end. Swift strings can't be indexed with integers, so there is no last[last.count - 3]. Instead, the standard library offers prefix(n), which keeps the first n characters, and suffix(n), which keeps the last n. You never need to compute a length or a start position.
The ID is then first.prefix(2) joined with last.suffix(3).
Step by step
- Take the first two letters of
firstwithfirst.prefix(2). - Take the last three letters of
lastwithlast.suffix(3). - Convert each piece to a
String. Both methods return aSubstring, a lightweight view into the original text, and the function returns aString. - Join the two strings with
+and return the result.
The solution in Swift
class Solution {
func creatingIds(_ first: String, _ last: String) -> String {
return String(first.prefix(2)) + String(last.suffix(3))
}
}
Details worth knowing:
- The underscores. The
_before each parameter name means callers pass the arguments without labels:creatingIds("John", "Smith")instead ofcreatingIds(first: "John", last: "Smith"). - Substring, then String. Joining two
Substringvalues with+gives anotherSubstring, which doesn't match the declared return type. Converting each piece withString(...)fixes that. - Counting characters.
prefixandsuffixcount Swift characters, not bytes. A letter with an accent written as two Unicode pieces still counts once. - Too-short names don't crash. If a name has fewer letters than requested,
prefixandsuffixreturn what is there. The lesson guarantees at least 2 and 3 letters, so this only matters if you reuse the idea elsewhere.
String interpolation gives an equivalent version: "\(first.prefix(2))\(last.suffix(3))". Interpolation turns each Substring into text for you, so no String(...) is needed. For "John" and "Smith" it also gives "Joith".
The ID has a fixed shape: it is always 5 characters long when the names meet the lesson's limits, 2 from the first name and 3 from the last. Any longer first name or last name only changes which letters are chosen, never the length.
Edge cases and common mistakes
- Exactly the minimum length.
"Jo"and"Doe"give"JoDoe". - Longer names.
"AL"and"LOVE"give"ALOVE": onlyOVE, the last three letters, are used. - Swapping the two calls.
last.prefix(2)withfirst.suffix(3)builds an ID from the wrong ends of the wrong names. - Changing the case. The ID keeps letters as written.
"ada"with"Lovelace"gives"adace", not"Adace". - Forgetting
String(...). Without it the compiler reports that it can't convert the returnedSubstringto aString.
Testing
"John","Smith"gives"Joith"."Jo","Doe"gives"JoDoe"."ada","Lovelace"gives"adace"."AL","LOVE"gives"ALOVE"."Bo","Li"is outside the lesson's constraints (the last name has only 2 letters), but it returns"BoLi": the code uses what is available.
