String manipulation types (Uppercase, Lowercase) in Typescript - Time & Space Complexity
We want to understand how the time it takes to change a string's letters to uppercase or lowercase grows as the string gets longer.
How does the work increase when the input string size increases?
Analyze the time complexity of the following code snippet.
function toUpperCaseExample(input: string): string {
return input.toUpperCase();
}
function toLowerCaseExample(input: string): string {
return input.toLowerCase();
}
This code converts all letters in a string to uppercase or lowercase using built-in methods.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The program checks each character in the string once to change its case.
- How many times: It repeats this operation for every character in the string, so as many times as the string length.
As the string gets longer, the time to convert all letters grows in a straight line with the number of characters.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 character checks |
| 100 | About 100 character checks |
| 1000 | About 1000 character checks |
Pattern observation: Doubling the string length roughly doubles the work needed.
Time Complexity: O(n)
This means the time to convert the string grows directly with the string length.
[X] Wrong: "Changing case is instant and does not depend on string size."
[OK] Correct: Each character must be checked and changed, so longer strings take more time.
Understanding how simple string operations scale helps you explain performance clearly and shows you know how computers handle text data.
"What if we changed the input to an array of strings and converted each string's case? How would the time complexity change?"
Practice
Solution
Step 1: Understand string case methods
TypeScript strings have built-in methods to change letter cases, including toUpperCase() and toLowerCase().Step 2: Identify method for uppercase
The method toUpperCase() changes all letters to uppercase, while toLowerCase() changes to lowercase.Final Answer:
toUpperCase() -> Option DQuick Check:
Uppercase method = toUpperCase() [OK]
- Confusing toUpperCase() with toLowerCase()
- Thinking toTitleCase() exists in TypeScript
- Assuming convertCase() is a valid method
name to lowercase in TypeScript?Solution
Step 1: Recall method call syntax on strings
In TypeScript, string methods are called on the string variable using dot notation.Step 2: Identify correct method name and usage
The method to convert to lowercase is toLowerCase(), called as name.toLowerCase(). Other options are invalid syntax or incorrect method names.Final Answer:
name.toLowerCase() -> Option AQuick Check:
Lowercase method called on variable = name.toLowerCase() [OK]
- Using function style like toLowerCase(name)
- Using incorrect method names like lowercase()
- Missing parentheses after method name
const text = "Hello World"; console.log(text.toUpperCase());
Solution
Step 1: Understand the input string and method
The string is "Hello World" and the method toUpperCase() converts all letters to uppercase.Step 2: Apply toUpperCase() to the string
Applying toUpperCase() changes "Hello World" to "HELLO WORLD".Final Answer:
"HELLO WORLD" -> Option CQuick Check:
toUpperCase() output = "HELLO WORLD" [OK]
- Expecting lowercase output instead
- Thinking original string is unchanged in output
- Confusing method name causing syntax error
let greeting = "Hi There"; console.log(greeting.toLowercase());
Solution
Step 1: Check method name spelling
The method to convert string to lowercase is toLowerCase() with capital 'C'. The code uses toLowercase() with lowercase 'c', which is invalid.Step 2: Verify other syntax elements
Semicolon is optional in TypeScript, let is valid for variable, and console.log is case-sensitive and correct as written.Final Answer:
toLowercase() is not a valid method; correct is toLowerCase() -> Option BQuick Check:
Method name case matters: toLowerCase() correct [OK]
- Ignoring case sensitivity in method names
- Thinking semicolon is mandatory error
- Confusing console.log capitalization
const fruits = ["Apple", "BANANA", "Cherry"];How do you create a new array where all fruit names are lowercase using TypeScript?
Solution
Step 1: Understand array transformation
To change each string in an array, use map() to create a new array with transformed values.Step 2: Apply toLowerCase() inside map callback
Inside map, call fruit.toLowerCase() to convert each string to lowercase. This returns a new array with all lowercase fruits.Final Answer:
fruits.map(fruit => fruit.toLowerCase()) -> Option AQuick Check:
Use map() with toLowerCase() for array lowercase [OK]
- Calling toLowerCase() directly on array
- Using forEach which returns void, not new array
- Trying to call toLowerCase() as a standalone function
Ready to prove it?
Pass the test and this pattern moves unknown
