0
0
Bash Scriptingscripting~5 mins

Uppercase and lowercase conversion in Bash Scripting - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Uppercase and lowercase conversion
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 script's work increase when the input text size increases?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

#!/bin/bash
input="$1"

# Convert to uppercase
uppercase=$(echo "$input" | tr '[:lower:]' '[:upper:]')

# Convert to lowercase
lowercase=$(echo "$input" | tr '[:upper:]' '[:lower:]')

echo "Uppercase: $uppercase"
echo "Lowercase: $lowercase"

This script takes a string input and converts it fully to uppercase and lowercase using the tr command.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: The tr command processes each character in the input string one by one.
  • How many times: It runs once for each character in the input string.
How Execution Grows With Input

As the input string gets longer, the script must convert more characters, so the work grows steadily.

Input Size (n)Approx. Operations
10About 20 character conversions
100About 200 character conversions
1000About 2000 character conversions

Pattern observation: The number of operations grows directly with the number of characters.

Final Time Complexity

Time Complexity: O(n)

This means the time to convert case grows in a straight line with the input size.

Common Mistake

[X] Wrong: "Changing case is instant and does not depend on input size."

[OK] Correct: Each character must be checked and changed, so longer text takes more time.

Interview Connect

Understanding how simple text operations scale helps you explain script efficiency clearly and confidently.

Self-Check

"What if we used a command that converts only vowels to uppercase? How would the time complexity change?"