Start learning free

LESSON 69 OF 291 · STRINGS - CHALLENGES

Amazing to Not Amazing in Kotlin

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

    KOTLIN · STRINGS - CHALLENGES

    Amazing To Not Amazing in Kotlin, explained

    Problem overview

    The function amazingToNotAmazing receives a word s that starts with a lower-case a and returns "not " followed by the word without its first character. The space is part of "not ".

    For "amazing" the answer is "not mazing". For "aMAZING" it is "not MAZING": only the first character is removed, and the rest keeps its case. For "an" it is "not n".

    Approach

    Two pieces are needed: the fixed prefix "not " and the word minus its first character. Kotlin strings have substring(startIndex), which returns everything from that index to the end. Starting at index 1 skips the first character. The + operator then joins the prefix and the rest.

    Step by step

    1. Write the prefix as the literal "not ", with the trailing space inside the quotes.
    2. Take s.substring(1), which is every character from index 1 onward.
    3. Join them with + and return the result.

    The solution in Kotlin

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

    Details worth knowing:

    • Strings are immutable. substring doesn't remove anything from s. It builds a new string and leaves s unchanged.
    • One argument means "to the end". substring(1) is a start index only. With two arguments, substring(start, end), the end index is exclusive.
    • Index equal to the length is allowed. "a".substring(1) is the empty string. Only an index past the length throws a StringIndexOutOfBoundsException. The lesson promises at least two characters, so the result is never empty.
    • Alternatives. s.drop(1) returns the same text, and a template "not ${s.substring(1)}" builds the same string.

    Tracing the examples

    For "amazing", s.substring(1) is "mazing". Joined to "not ", it gives "not mazing". The first letter was thrown away, which is the whole trick of the exercise.

    For "aMAZING", s.substring(1) is "MAZING", so the answer is "not MAZING". substring copies characters exactly, so no upper-case letter turns lower case.

    For "an", s.substring(1) is "n", and the answer is "not n". A one-character remainder is as valid as a long one.

    The lesson says every word starts with a, and the function never checks it. It removes whatever is at index 0, so the rule lives in the input, not in the code.

    Other ways to write it

    The reference solution uses +, which is the clearest choice for two pieces. A string template does the same job: "not ${s.substring(1)}". And s.drop(1) returns the string without its first character, so "not " + s.drop(1) is also valid. All three give "not mazing" for "amazing".

    Whichever you pick, the idea is the same: count positions from 0, skip the first one, and keep the rest. When a task says "without its first character", think of index 1 as the new start. When it says "without its last character", think of substring(0, s.length - 1) instead.

    Edge cases and common mistakes

    • Forgetting the space. "not" + s.substring(1) gives "notmazing".
    • Starting at 0. s.substring(0) is the whole word, so "amazing" would become "not amazing".
    • Changing the case. The result keeps the letters as they are. "aMAZING" must give "not MAZING".
    • Replacing instead of cutting. The task removes the first character by position. It doesn't depend on which letter it is.

    Testing

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