A string is just an ordered list of characters. Two operations start everything:
- its length — how many characters it holds.
- indexing — reach a single character by position, counting from 0. In
"code", position 0 isc, position 1 iso, and the last is at indexlength - 1.
That zero-based counting is the number-one beginner tripwire: the last valid index is
length - 1, not length. Reaching index length runs off the end.
def solve(s):
return len(s) # the length of the string
static long solve(String s) {
return s.length();
}
Some problems read a single word (no spaces); others read a whole line that may contain
spaces. Either way your solve receives the text as a string, and the harness prints what you
return. The mental model — a string is a sequence you can measure and index by position — is
the base every string problem builds on.