The String struct doesn’t provide a count or length property or method to count the number of characters it contains. Instead a global countElements<T>() function is available. When applied to strings, what’s the complexity of the countElements function:
O(1)
O(n)
and why?
Answer Posted / iosraj
Swift strings support extended grapheme clusters. Each character stored in a string is a sequence of one or more unicode scalars that, when combined, produce a single human readable character. Since different characters can require different amounts of memory, and considering that an extreme grapheme cluster must be accessed sequentially in order to determine which character it represents, it’s not possible to know the number of characters contained in a string upfront, without traversing the entire string. For that reason, the complexity of the countElements function is O(n).
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
What is property observer in swift?
What are structures in swift?
What is forced unwrapping?
Which is better car baleno or swift?
How can we make a property optional in swift?
Is swift hard to learn?
What is a string swift?
Why swift is the best language?
What is atomic swift?
What is string in swift?
Who created swift?
What is dynamic keyword in swift?
In swift, what type of object are basic data types?
Is swift object oriented?
What is closure in swift?