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. For "aMAZING" the result is "not MAZING", because the other letters keep their case. For "an" the result is "not n".
Approach
The task has two parts: drop the first character, and put a fixed prefix in front of what is left. Python strings are immutable, so you can't delete a character in place. Instead you build a new string from a slice of the old one. A slice s[1:] starts at index 1 and, because the end index is left out, runs to the end of the string.
A good way to check your work is to think about what each part is responsible for. The prefix "not " never depends on the input, so it is a fixed string. The slice depends only on the word, and it always drops exactly one character. If the result for "an" is "not n" and the result for "amazing" is "not mazing", both parts are doing their job.
Step by step
- Slice from index 1.
s[1:]is every character except the one at index0. - Add the prefix. Write
"not "with its trailing space inside the quotes, then join it to the slice with+. - Return the result.
The solution in Python
class Solution:
def amazingToNotAmazing(self, s: str) -> str:
return "not " + s[1:]
Some details worth knowing:
- The slice
s[1:]returns a new string and leavessas it was."amazing"[1:]is"mazing". - Slices never raise an error for going past the ends. That matters little here, since the word has at least two characters, but a one-character word would simply give an empty slice.
- The space is part of the prefix. Without it, the result would be
"notmazing".
How the slice picks characters
A slice s[start:end] keeps the characters from index start up to, but not including, index end. Leaving out end means "to the end", and leaving out start means "from the beginning". For w = "amazing":
w[1:]is"mazing": everything from index 1 on.w[0:]is"amazing": the whole word.w[:1]is"a": the part this lesson throws away.w[3:5]is"zi": a slice with both bounds.
The first character and the slice from index 1 always add up to the original word, which is a handy check: w[:1] + w[1:] is w.
The same idea with a named variable
You can also keep the remaining letters in a variable before adding the prefix. For w = "aMAZING" the code below prints MAZING, then not MAZING, then aMAZING:
w = "aMAZING"
rest = w[1:]
print(rest)
print("not " + rest)
print(w)
The last line shows that w is unchanged: slicing made a new string and the original is still there. That is how you "remove" a character from a Python string: you never edit it, you build a shorter copy.
Edge cases and common mistakes
- Forgetting the space.
"not" + s[1:]glues the words together. - Slicing from index 0.
s[0:]is the whole string, so theastays:"not amazing". - Changing case. Don't call
lower()orupper(). The letters after the first stay exactly as they are. - Trying to assign into the string.
s[0] = ""raises aTypeError, because strings don't support item assignment.
Testing
"amazing"gives"not mazing"."aMAZING"gives"not MAZING"."an"gives"not n".
