Saturday, September 16, 2017

Interview Question - Find an equal point in a string of brackets

Source Link:
https://www.careercup.com/question?id=5633243081605120

Question: 
Given a string of brackets, the task is to find an index k which decides the number of opening brackets is equal to the number of closing brackets.
String must be consists of only opening and closing brackets i.e. ‘(‘ and ‘)’.

An equal point is an index such that the number of opening brackets before it is equal to the number of closing brackets from and after.



Space Complexity O(n) Solution:
http://www.geeksforgeeks.org/find-equal-point-string-brackets/

Space Complexity O(1) Solution:
https://github.com/Jarosh/Exercises/wiki/Split-array-into-two-parts,-such-that-the-number-of-elements-equal-to-X-in-the-first-part-is-the-same-as-the-number-of-elements-different-from-X-in-the-other-part

The O(1) Trick:
The answer is always:  "length_of_the_string - num_of_opening_brackets"


No comments:

Post a Comment

Codility - Lesson 16 Greedy algorithms - 2. MaxNonoverlappingSegments

Source Link: https://app.codility.com/programmers/lessons/16-greedy_algorithms/max_nonoverlapping_segments/ Question: Located on a line ...