0
0
PHPprogramming~5 mins

Case conversion functions in PHP - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Case conversion functions
O(n)
Understanding Time Complexity

We want to understand how the time it takes to change text case grows as the text gets longer.

How does the program's work increase when we convert bigger strings?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


$string = "Hello World!";
$lower = strtolower($string);
$upper = strtoupper($string);

// Convert each character to lowercase and uppercase

This code converts a string to all lowercase and all uppercase letters.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: The functions check each character in the string one by one.
  • How many times: Once for each character in the string.
How Execution Grows With Input

As the string gets longer, the work 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.

Final Time Complexity

Time Complexity: O(n)

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

Common Mistake

[X] Wrong: "Changing case is instant no matter the 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 efficiency clearly and confidently.

Self-Check

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