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
How do you make a swift bridging header?
What is hashable in swift?
How do I add a header in swift?
How can you declare a variable in swift?
What is a static variable swift?
What are floating point numbers? How many types of floating number are there?
What is tuple in swift?
What are the half open range operators in swift?
Is swift dynamic or static?
What is a guard statement? What is the benefit of using guard statement in swift?
What is bridging header in swift?
What is mutating keyword in swift?
How should errors be handled in swift?
What are the control transfer statements that are used in ios swift?
What is static let in swift?