Start learning free

LESSON 68 OF 671 · STRINGS - CHALLENGES

Amazing to Not Amazing in Java: substring(1)

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

    JAVA · STRINGS - CHALLENGES

    Amazing To Not Amazing in Java, explained

    Problem overview

    The method amazingToNotAmazing receives a word s that starts with a lower-case a. It returns "not ", with its trailing space, followed by s without its first character. That is how "amazing" becomes "not mazing".

    Only the first character is removed, and nothing else changes: "aMAZING" gives "not MAZING", with the upper-case letters intact, and "an" gives "not n".

    Approach: drop one character, add a prefix

    The task has two parts: build the rest of the word without its first letter, and put a fixed prefix in front. Java strings are immutable, so you can't delete the first character from s. Instead you ask for a new string that contains the characters you want to keep.

    The method for that is substring. Called with one argument, s.substring(1) returns everything from index 1 to the end of the string. Index 0 is the a, so index 1 is where the kept part begins. With "amazing" that gives "mazing".

    The prefix is the literal "not ". The space is part of it: without the space, "amazing" would become "notmazing". The + operator joins two strings, so the whole answer is the prefix plus the slice.

    Step by step

    1. Cut off the first character. s.substring(1) starts at index 1 and runs to the end. A single start index is enough, because the end defaults to the length of the string, so there is no need to compute s.length().
    2. Write the prefix. The string "not " has four characters: three letters and a space.
    3. Join and return. "not " + s.substring(1) is a single expression, and the method returns its value directly.

    The solution in Java

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

    Running it on the three examples prints:

    amazing -> not mazing
    aMAZING -> not MAZING
    an -> not n
    

    A few Java details matter here:

    • substring returns a new string. After the call, s still holds the whole word. Calling s.substring(1); as a statement and ignoring the result does nothing useful, because the original is never modified.
    • Start index included, end index excluded. With two arguments, substring(1, 3) on "amazing" returns "ma": the characters at indexes 1 and 2. The one-argument form has no end to worry about.
    • + on strings. When one side of + is a String, Java concatenates. This is why the expression works even though the first operand is a literal.

    Edge cases and common mistakes

    • Forgetting the space. "not" + s.substring(1) gives "notmazing". The statement shows "not mazing" with a space between the words.
    • Using substring(0). Index 0 is where the string begins, so substring(0) returns the whole word and the a stays: "not amazing".
    • Using replace. s.replace("a", "") would remove every a in the word, not only the first character, and "amazing" would lose its second a too.
    • Changing the case. The rest of the word keeps its letters exactly as they are, so no toLowerCase is needed: "aMAZING" must give "not MAZING".
    • The shortest word. For "an", substring(1) is "n". If the word were a single "a", the slice would be the empty string and the result would be "not ", but the constraints guarantee two characters or more.

    Testing

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