string split in java time complexity code example

Example: time complexity of split java

8

The complexity will depend on the regex that you use to do the splitting. (Yes, the argument you supply to String.split(...) is a regex!)

For your example, it will be O(N) where N is the number of characters in the input String.

The algorithm of split is pretty straight forward, based on an existing regex implementation. A high-level description is:

Compile the regex and create a matcher
Iterate over the string:
Use Matcher.find(...) to find the next word boundary
Use String.substring to extract the word
Add word to a list of strings
Convert the list of strings to an array of strings.

Tags:

Java Example