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
- 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 computes.length(). - Write the prefix. The string
"not "has four characters: three letters and a space. - 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:
substringreturns a new string. After the call,sstill holds the whole word. Callings.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 aString, 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, sosubstring(0)returns the whole word and theastays:"not amazing". - Using
replace.s.replace("a", "")would remove everyain the word, not only the first character, and"amazing"would lose its secondatoo. - Changing the case. The rest of the word keeps its letters exactly as they are, so no
toLowerCaseis 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".
