Arrays and Strings
The loops, counters and in-place tricks that almost every easy and medium problem is built from.
The problem
You are given a string such as abracadabra. How many different letters does it contain?
That one question already needs the three habits of this lesson:
- Walk through the data in a controlled way.
- Count things in a small table instead of searching for them.
- Change data in place when memory or time is tight.
An array is a row of values stored one after another in memory, numbered from index 0 to n - 1. A string is an array of characters with a few extra tools. Reading a[i] costs the same for any i, which is called O(1), or constant time.
1 of 11