Problem overview
The function amazingToNotAmazing receives a word s that starts with a lowercase a, and returns "not " followed by the word without its first character. That is how "amazing" becomes "not mazing".
Only the first character is removed, and the others stay as they are: "aMAZING" gives "not MAZING". For "an" the result is "not n". The space inside "not " is part of the answer.
Approach: drop one character, then add a prefix
The task has two small jobs: remove the first character, and put a fixed piece of text in front of what remains. Swift has a method for the first job. dropFirst() returns everything except the first character, and dropFirst(n) would skip n characters. For the second job, + joins strings.
Step by step
- Remove the first character with
s.dropFirst(). - Convert the rest to a
String.dropFirst()returns aSubstring, a view of part of the original string, and the function's return type isString. - Put
"not "in front with+, and return the result. The trailing space is inside the quotes: without it,"amazing"would become"notmazing".
The solution in Swift
class Solution {
func amazingToNotAmazing(_ s: String) -> String {
return "not " + String(s.dropFirst())
}
}
Some details:
- No indexes needed. A Swift
Stringcan't be indexed with an integer, so there is nos[1...]. Methods such asdropFirst()do the walking for you. - Substring versus String. A
Substringshares its storage with the string it came from.String(...)copies it into a string of its own, which is what the lesson asks you to return. Joining aStringand aSubstringwith+also gives aString, but the explicit conversion makes the types clear. - It is safe on short input.
dropFirst()on an empty string gives an empty string instead of crashing, though the lesson's words always have at least two characters. - Case is untouched. The method removes a character and changes nothing else, so uppercase letters in the rest of the word stay uppercase.
Related methods
dropFirst has relatives that help whenever a string needs trimming. All of them return a Substring. With w = "amazing":
w.dropFirst(2)isazing: it skips two characters.w.dropLast()isamazin: it removes the last character.w.dropLast(3)isamaz: it removes three from the end.
None of them traps when n is bigger than the string: the result is just empty. That makes them safer than slicing with computed numbers, and they pair naturally with prefix and suffix, which keep characters from the front or the back instead of removing them.
Edge cases and common mistakes
- The shortest word.
"an"leaves"n", so the result is"not n". - Missing space.
"not"instead of"not "glues the prefix to the word. - Converting too late or not at all. A
Substringreturned as it is doesn't match the return typeString. Wrap it inString(...)before returning it. - Changing the case. Don't lowercase or uppercase anything:
"aMAZING"must keep its capitals.
Testing
"amazing"gives"not mazing"."aMAZING"gives"not MAZING"."an"gives"not n"."aa"gives"not a": only the firstais removed."abc"gives"not bc".
