0
0
Typescriptprogramming~5 mins

String manipulation types (Uppercase, Lowercase) in Typescript - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: String manipulation types (Uppercase, Lowercase)
O(n)
Understanding Time 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?

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

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
10About 10 character checks
100About 100 character checks
1000About 1000 character checks

Pattern observation: Doubling the string length roughly doubles the work needed.

Final Time Complexity

Time Complexity: O(n)

This means the time to convert the string grows directly with the string length.

Common Mistake

[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.

Interview Connect

Understanding how simple string operations scale helps you explain performance clearly and shows you know how computers handle text data.

Self-Check

"What if we changed the input to an array of strings and converted each string's case? How would the time complexity change?"