Start learning free

LESSON 69 OF 291 · STRINGS - CHALLENGES

Amazing to Not Amazing in Scala

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

    SCALA · STRINGS - CHALLENGES

    Amazing To Not Amazing in Scala, explained

    Problem overview

    The method amazingToNotAmazing receives a word s that starts with a lower-case a and returns "not " followed by s without its first character. The word "amazing" becomes "not mazing". Only the first character is removed: "aMAZING" becomes "not MAZING", with the upper-case letters untouched. And "an" becomes "not n".

    Approach

    Two pieces make up the result: the fixed text "not " and the rest of the word. The rest of the word is everything from index 1 onward, because index 0 holds the a to drop. Scala's substring with a single argument does exactly that: it starts at the given index and goes to the end of the string.

    The space belongs inside the string literal. "not " with a trailing space gives "not mazing", while "not" would give "notmazing".

    How indexes work

    Every character in a Scala string has a position, counted from 0. In "amazing", the a is at index 0, m at 1, a at 2 and so on. substring(1) means "start at index 1", so index 0 is skipped and everything after it is kept. The same call works for a word of any length, since the end of the string is the default end of the slice.

    The problem could also be solved by removing the first character with s.drop(1), which is another way to write the same slice. This lesson uses substring, which makes the start index explicit.

    Step by step

    1. Cut off the first character with s.substring(1). For "amazing" this is "mazing".
    2. Put "not " in front with +, and return the joined string.

    The solution in Scala

    class Solution {
        def amazingToNotAmazing(s: String): String = {
            return "not " + s.substring(1)
        }
    }
    

    A few Scala details:

    • Strings are immutable. substring doesn't remove anything from s: it returns a new string, and s keeps its original value.
    • + between two strings joins them. Here the left side is the literal "not " and the right side is the slice.
    • The method never checks that the word starts with a. It drops whatever the first character is, which is fine because the lesson guarantees an a.

    Tracing an example

    With s = "amazing", the call s.substring(1) returns "mazing", and s itself is still "amazing" afterwards. Joining "not " and "mazing" gives "not mazing".

    The two small mistakes show up clearly on the same word. With "not" instead of "not ", the result is "notmazing". With s.substring(0) in place of s.substring(1), the result is "not amazing". Checking these two against the expected output is a quick way to see which index and which literal the answer needs.

    Edge cases and common mistakes

    • Forgetting the space. Without the space inside "not ", the output runs the two words together.
    • Starting the slice at 0. s.substring(0) is the whole word, so the a stays: "not amazing".
    • Changing case. Only the first character is removed. Nothing is converted to lower or upper case.
    • Shortest input. With two letters, such as "an", the slice is the single letter "n", so the answer is "not n".

    Testing

    • "amazing" gives "not mazing".
    • "aMAZING" gives "not MAZING".
    • "an" gives "not n".
    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free