1

According to Time complexity of Java's substring(), java's substring takes linear time. Is there a faster way (may be in some cases)? I may suggest iterator, but suspect that it also takes O(n).

val s1: String  = s.iterator.drop(5).mkString

But several operations on an iterator would be faster than same operations on string, right?

2 Answers 2

5

If you need to edit very long string, consider using data structure called Rope.

Scalaz library has Cord class which is implementation of modified version of Rope.

A Cord is a purely functional data structure for efficiently storing and manipulating Strings that are potentially very long. Very similar to Rope[Char], but with better constant factors and a simpler interface since it's specialized for Strings.

Sign up to request clarification or add additional context in comments.

Comments

1

As Strings are - according to the linked question - always backed by a unique character array, substring can't be faster than O(n). You need to copy the character data.

As for alternatives: there will at least be one operation which is O(n). In your example, that's mkString which collects the characters in the iterator and builds a string from them.

However, I wouldn't worry about that too much. The fact that you're using a high level language means (should mean) that developer time is more valuable than CPU time for your particular task. substring is also the canonical way to ... take a substring, so using it makes your program more readable.

EDIT: I also like this sentence (from this answer) a lot: O(n) is O(1) if n does not grow large. What I take away from this is: you shouldn't write inefficient code, but asymptotical efficiency is not the same as real-world efficiency.

2 Comments

Totally agree, but what about large strings (like 1M or larger)?
@ChelovekChelovechnii In that case, String maybe isn't for you. I'd edit the question to clarify that you are not asking about Strings exclusively, but any data structures for large character sequences.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.