Start learning free

LESSON 69 OF 292 · STRINGS - CHALLENGES

Amazing to Not Amazing in Swift: dropFirst

Practice Read the full explanation

Given a word s that starts with the letter a, return "not " followed by s without its first character. That's how "amazing" becomes "not mazing"!

 

Example 1:

Input: s = "amazing"
Output: "not mazing"
Explanation: Without its first character, "amazing" is "mazing". "not " goes in front of it, with its space.

Example 2:

Input: s = "aMAZING"
Output: "not MAZING"
Explanation: Only the first character is removed. The other characters stay as they are, upper case included.

Example 3:

Input: s = "an"
Output: "not n"
Explanation: Removing the "a" leaves just "n".

 

Constraints:

  • s has at least 2 characters and starts with a lower-case a.
  • s is one word made of English letters, lowercase or uppercase (no spaces).
STEP 1 OF 1
    Ctrl + Enter to check

    SWIFT · STRINGS - CHALLENGES

    Amazing To Not Amazing in Swift, explained

    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

    1. Remove the first character with s.dropFirst().
    2. Convert the rest to a String. dropFirst() returns a Substring, a view of part of the original string, and the function's return type is String.
    3. 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 String can't be indexed with an integer, so there is no s[1...]. Methods such as dropFirst() do the walking for you.
    • Substring versus String. A Substring shares 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 a String and a Substring with + also gives a String, 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) is azing: it skips two characters.
    • w.dropLast() is amazin: it removes the last character.
    • w.dropLast(3) is amaz: 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 Substring returned as it is doesn't match the return type String. Wrap it in String(...) 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 first a is removed.
    • "abc" gives "not bc".
    Keep going with Swift1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free