Question: HackerRank OA Coding Question Solved | Balanced Bracket Adjustments
0
Entering edit mode

Question 1: Convertible Balanced Bracket Sequence

Problem Statement:

Given a dataset of strings containing only parentheses, characters '(' and ')', the data represented by the string is valid if it is a balanced bracket sequence.One adjustment to the string can be made: at most one bracket can be moved from its original place to any other position in the string. The task is to determine whether, for each string, it is possible to balance the bracket sequence in 1 move or less. Return an array of the size of the dataset, where the i^th integer is 1 if the string can be converted into a balanced string, and 0 otherwise.

Note: A string s is a balanced bracket sequence if:

  • s is empty.
  • s is equal to "(t)", where t is a balanced bracket sequence.
  • s is equal to t_1t_2, i.e., concatenation of t_1 and t_2 where t_1 and t_2 are balanced bracket sequences.

Example:

n = 3

dataset = [")(", "(()", "()"]

  • For the first string ")(", applying the operation to move the first bracket to the end results in "()", which is a balanced bracket sequence. (Result: 1)
  • For the second string "(()", it is impossible to convert it into a balanced bracket sequence. (Result: 0)
  • For the third string "()", it is already a balanced bracket sequence. (Result: 1)

Hence, the answer is [1, 0, 1].

Login before adding your answer.

Similar Posts
Loading Similar Posts