-6

what is the running time

   String[] stringCombo = new String[5];
    for (int i = 0; i < stringCombo.length; i++) {
        stringCombo[i] = deleteCharAt(s,i);
        s = original;
    }

i am not sure if it is O(N), because the length is fixed to be 5.

private static String deleteFirstChar(String s) {
    return s.substring(1);
}
Doejo
  • 1
  • 2

1 Answers1

1

Here is the Java source for [String]. Look at substring and you will find that the order of substring(..) is O(1) (before Java 6). [[For Java >= 7, substring actually involves copying of the (sub)array which is an O(N) operation (see Paul Boddington's comment below)]]

You are doing this 5 times, which is fixed. So, regardless of N(?), this code will run 5 times.

Q: Do you think this depends on N?

A: No.

Order is O(1).

Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43
  • You're looking at an old version of `substring`. It used to be `O(1)`, but now it's linear in the number of characters because it copies the array. http://stackoverflow.com/questions/16123446/java-7-string-substring-complexity – Paul Boddington Apr 14 '16 at 23:37
  • 1
    Oh yes! Copying of arrays is an `O(N)` operation for Java >= 7. I keep making this mistake! Thanks, @PaulBoddington. I'll edit it. – Debosmit Ray Apr 14 '16 at 23:38