String as character array in Data Structures Theory - Time & Space Complexity
When we treat a string as a list of characters, it helps to understand how operations on it grow as the string gets longer.
We want to know how the time needed changes when we do things like reading or searching characters in the string.
Analyze the time complexity of the following code snippet.
function findChar(str, target) {
for (let i = 0; i < str.length; i++) {
if (str[i] === target) {
return i;
}
}
return -1;
}
This code searches for a character in a string treated as an array of characters and returns its position or -1 if not found.
- Primary operation: Looping through each character in the string one by one.
- How many times: Up to the length of the string, stopping early if the character is found.
As the string gets longer, the number of characters checked grows roughly the same as the string length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 character checks |
| 100 | Up to 100 character checks |
| 1000 | Up to 1000 character checks |
Pattern observation: The work grows directly with the string length; doubling the string roughly doubles the work.
Time Complexity: O(n)
This means the time to find a character grows linearly with the string length.
[X] Wrong: "Searching a character in a string is always instant or constant time."
[OK] Correct: Because the search may need to check many characters one by one, the time depends on the string length.
Understanding how string operations scale helps you explain your code choices clearly and shows you know what happens behind the scenes.
"What if the string was stored as a linked list of characters instead of an array? How would the time complexity change?"
Practice
string as a character array?Solution
Step 1: Understand string as sequence of characters
A string is made up of letters arranged in order, like beads on a string.Step 2: Accessing letters by position
Treating a string as a character array means you can get each letter by its place number (index).Final Answer:
Accessing each letter individually by its position -> Option DQuick Check:
String as character array = access letters by position [OK]
- Confusing string with list of words
- Thinking string stores numbers only
- Believing strings cannot be accessed by position
s in most programming languages?Solution
Step 1: Recall zero-based indexing
Most languages start counting positions from 0, so first character is s[0], second s[1], third s[2].Step 2: Identify correct syntax for indexing
Square brackets[]are used to access characters by index, sos[2]is correct.Final Answer:
s[2] -> Option AQuick Check:
Third character index = 2, use s[2] [OK]
- Using parentheses instead of brackets
- Using 1-based indexing (s[3])
- Using curly braces for indexing
word = "hello", what is the output of word[1]?Solution
Step 1: Identify the string characters and their indexes
The string "hello" has characters: h(0), e(1), l(2), l(3), o(4).Step 2: Access character at index 1
Index 1 corresponds to the second character, which is "e".Final Answer:
"e" -> Option BQuick Check:
word[1] = "e" [OK]
- Choosing first character instead of second
- Confusing index 1 with index 2
- Thinking indexing starts at 1
text = "world" and we try to print text[5]?Solution
Step 1: Check string length and valid indexes
The string "world" has length 5, with valid indexes 0 to 4.Step 2: Understand index out of range error
Accessing index 5 is beyond the last character, causing an error in most languages.Final Answer:
It will cause an error because index 5 is out of range -> Option AQuick Check:
Index 5 invalid for length 5 string [OK]
- Assuming index 5 returns last character
- Expecting a space or empty character
- Thinking indexing starts at 1
name = "alice" to uppercase by treating it as a character array. Which approach is correct?Solution
Step 1: Understand string immutability
In many languages, strings cannot be changed directly because they are immutable.Step 2: Use list conversion to modify characters
Convert the string to a list (array) of characters, change the first element, then join back to a string.Final Answer:
Convert string to list, change first element, then join back -> Option CQuick Check:
Modify string by list conversion and join [OK]
- Trying to assign directly to string index
- Using upper() on whole string but expecting partial change
- Believing strings are mutable
Ready to prove it?
Pass the test and this pattern moves unknown
