Char type and Unicode behavior in Kotlin - Time & Space Complexity
We want to understand how operations with Kotlin's Char type and Unicode characters affect the time it takes to run code.
Specifically, how does processing characters scale when dealing with Unicode?
Analyze the time complexity of the following code snippet.
fun countUppercase(chars: List): Int {
var count = 0
for (c in chars) {
if (c.isUpperCase()) {
count++
}
}
return count
}
This code counts how many uppercase characters are in a list of characters, considering Unicode rules.
- Primary operation: Looping through each character in the list once.
- How many times: Exactly once per character, so as many times as the list size.
As the number of characters grows, the code checks each one once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 character checks |
| 100 | 100 character checks |
| 1000 | 1000 character checks |
Pattern observation: The work grows directly with the number of characters.
Time Complexity: O(n)
This means the time to count uppercase characters grows linearly with the number of characters.
[X] Wrong: "Checking if a character is uppercase takes constant time regardless of Unicode complexity."
[OK] Correct: Some Unicode characters require more work to check case, but Kotlin handles this internally, so the overall loop still runs once per character.
Understanding how character processing scales helps you write efficient text handling code, a common task in many programs.
"What if we changed the input from a List<Char> to a String? How would the time complexity change?"
Practice
Char variable in Kotlin?Solution
Step 1: Understand Char declaration syntax
In Kotlin, a Char value must be enclosed in single quotes, like 'A'.Step 2: Check each option
val letter: Char = 'A' uses single quotes correctly. val letter: Char = "A" uses double quotes (String). val letter: Char = A has no quotes (invalid). val letter: Char = `A` uses backticks (invalid for Char).Final Answer:
val letter: Char = 'A' -> Option DQuick Check:
Char uses single quotes = A [OK]
- Using double quotes instead of single quotes
- Omitting quotes around the character
- Using backticks instead of quotes
Char in Kotlin?Solution
Step 1: Recall Kotlin Char Unicode property
Kotlin provides thecodeproperty to get the Unicode integer value of a Char.Step 2: Evaluate options
val code = 'A'.code usescodeproperty correctly. val code = 'A'.toInt() usestoInt()which does not exist for Char. Options B and D use non-existent methods/properties.Final Answer:
val code = 'A'.code -> Option AQuick Check:
Use .code to get Unicode number [OK]
- Using non-existent toInt() method on Char
- Trying non-existent methods like unicodeValue()
- Accessing properties that don't exist
val ch: Char = 'Z' println(ch.code)
Solution
Step 1: Identify the Char and property used
The variablechis assigned the character 'Z'. The code printsch.code, which returns the Unicode integer value.Step 2: Find Unicode value of 'Z'
The Unicode code for uppercase 'Z' is 90.Final Answer:
90 -> Option BQuick Check:
'Z'.code = 90 [OK]
- Confusing uppercase and lowercase Unicode values
- Expecting the character itself instead of code
- Thinking code property causes syntax error
val ch: Char = "A" println(ch.code)
Solution
Step 1: Check Char assignment syntax
Chars must be assigned using single quotes, not double quotes. Double quotes create a String.Step 2: Verify other parts
Semicolons are optional in Kotlin, so no error there. Thecodeproperty exists for Char. Variable type Char is correct.Final Answer:
Using double quotes for Char assignment -> Option CQuick Check:
Char needs single quotes, not double [OK]
- Thinking semicolons are mandatory
- Believing code property is invalid
- Confusing Char and Int types
val chars = listOf('a', 'B', 'c', 'D'), which Kotlin expression correctly filters only uppercase letters using Unicode properties?Solution
Step 1: Understand filtering uppercase letters
Kotlin's Char class has a methodisUpperCase()that returns true if the character is uppercase.Step 2: Evaluate each option
chars.filter { it.isUpperCase() } usesisUpperCase()correctly. chars.filter { it.code > 90 } filters by code > 90, which excludes all uppercase letters (A-Z: 65-90). chars.filter { it.code.isUpperCase() } callsisUpperCase()on an Int (code), which does not exist. chars.filter { it > 'Z' } compares chars incorrectly.Final Answer:
chars.filter { it.isUpperCase() } -> Option AQuick Check:
Use isUpperCase() to filter uppercase chars [OK]
- Filtering by numeric code incorrectly
- Calling isUpperCase() on the Unicode code (Int)
- Using wrong comparison operators for chars
Ready to prove it?
Pass the test and this pattern moves unknown
