user2954463
user2954463

Reputation: 2401

What is the Big-O analysis of my recursive function?

Preparing for technical interviews, I solved a practice problem with a recursive solution.

What is the runtime complexity of a recursive function such as this? I am more concerned with the explanation rather than the answer.

From my analysis- the number of operations is going to be half of n. That is, a string of 10 characters is going to take 5 function calls in the worst case scenario. But I have never seen an O(n/2) runtime. Also, my analysis excludes the call to the helper function counterpartOf. Could someone please show me a proper analysis?

Write a function that accepts a string consisting of brackets ({}) and returns whether it is balanced.

function checkBraces(input){
  // start at the center and work outwards, recursively
  var c = input.length / 2;

  if (input.charAt(c) !== counterpartOf(input.charAt(c-1))) {
    var match = false;
    return match;
  } else {
    // if only 2 characters are left, all braces matched
    if (input.length === 2){
      var match = true;
      return match;
    } else {
      input = input.substring(0,c-1) + input.substring(c+1,input.length);
      return checkBraces(input);
    }
  }
  return match;
}

function counterpartOf(brace){
  closing = ['}', ')', ']'];
  opening = ['{', '(', '['];
  var i = opening.indexOf(brace);
  var counterpart = closing[i];
  return counterpart;
}

Upvotes: 4

Views: 609

Answers (3)

Tom McQuarrie
Tom McQuarrie

Reputation: 1147

Kind of off topic, and not sure if you were required to use recursion, but I think there's a far more efficient way to get the result:

function checkBraces( input )
{
   // if the string is an odd number of characters, return immediately.
   if( input.length % 2 !== 0 ) return false; 

   // split the string in half
   var c = input.length / 2;
   var r = input.substr( c );
   var l = input.substr( 0, c );

   // take the left side, reverse it, and swap each left bracket character with it's counterpart
   l = l.split('').reverse().join('').replace(/\{|\(|\[/g, counterpartOf );

   // strings should match
   return r == l;
}

Upvotes: 1

Yevgeniy.Chernobrivets
Yevgeniy.Chernobrivets

Reputation: 3194

Complexity of your function will be O(n) only in case if javascript substring function takes constant time. If complexity of substring function is O(k) where k is length of substring then complexity of your function will be O(n^2). You need to check implementation of javascript substring function to be sure.

Upvotes: 1

Lajos Veres
Lajos Veres

Reputation: 13725

Constants are irrelevant, this is why you won't see /2, *2 or anything similar.

Details:

http://en.wikipedia.org/wiki/Big_O_notation#Multiplication_by_a_constant

O(k*g) = O(g) if k is non zero.

Otherwise as Yevgeniy.Chernobrivets mentioned your algorithm is not accurate. But apart from his comment I think there are other problems as well.

Usually for similar tasks, they use push down automata's. There is some theoretical background about the issue: http://people.cs.clemson.edu/~goddard/texts/theoryOfComputation/7.pdf

Upvotes: 1

Related Questions