xyz
 0of 0 votes
AnswersYou are given a string S consisting of N brackets, opening "("
 xyz in United States
and/or closing ")". The goal is to split S into two parts (left and
right), such that the number of opening brackets in the left part is
equal to the number of closing brackets in the right part.
For example, given S = "(())", the function should return 2,
because:
the first two characters of S, "((", contain two
opening brackets, and
the remaining two characters of S, "))", contain
two closing brackets.
In other example, given S = "(())))(", the function should return
4, because:
the first four characters of S, "(())", contain two
opening brackets, and
the remaining three characters of S, "))(", contain
two closing brackets.
In other example, given S = "))", the function should return 2,
because:
the first two characters of S, "))", contain zero
opening brackets, and
there are no remaining characters, so they contain
also zero closing brackets. Report Duplicate  Flag
Arrays  0of 0 votes
Answersgiven 2 Dimensional array
 xyz in United States
I/P  String[][] input = { { "abc", "def", "gh" },
{ "f", "g" },
{ "qrt","xyz","pqr" } };
Program shd return a 2D Array with
O/P  { { "abcfqrt", "abcfxyz", "abcfpqr" ,abcgqrt and so on .. Report Duplicate  Flag
Google SDE2 Arrays
public static Node reverse(Node head, int k) {
Node temp = head;
Node prevTemp = temp;
Node out = null;
Node newNode = null;
if (head != null)
{
temp = head;
int i = 0;
while (temp.Next!=null) {
if(i == k)
break;
prevTemp = temp;
temp = temp.Next;
i++;
}
prevTemp.Next = null;
newNode = reverse(head);
out = merge(newNode,temp);
}
return out;
}
public static Node reverse(Node node) {
if (node == null)
return null;
else if (node.Next == null)
return node;
else {
Node second = node.Next;
node.Next = null;
Node reverseRest = reverse(second);
second.Next = node;
return reverseRest;
}
}
private static Node merge(Node node1, Node node2) { // 3,2,1  4,5
Node temp = node1;
if(temp.Next == null)
return temp.Next = node2;
else {
merge(temp.Next, node2);
}
return temp;
}

xyz
April 30, 2017 I was able to write this way but looks like this is not correct approach
public static void main(String args[]) {
int k = solution (")(");
System.out.println(k);
}
private static int solution(String string) {
if(!string.contains("("))
return string.length();
else if(!string.contains(")"))
return 0;
char[] input = string.toCharArray();
int closeCount = 0;
int openCount = 0;
for (int i = 0; i<input.length;i++) {
if(input[i] == '(')
openCount++;
closeCount = 0;
for(int j =i+1;j<input.length;j++){
if(input[j] == ')')
closeCount++;
}
if (openCount == closeCount&&openCount!=0&&closeCount!=0)
return (i+1);
}
return 0 ;
}
 xyz March 17, 2017Open Chat in New Window
 xyz May 12, 2017