Advertisement
Guest User

Untitled

a guest
Jan 11th, 2017
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.82 KB | None | 0 0
  1.  public static int solution(String S) {
  2.  
  3.         for(int i=1; i<S.length();i++){
  4.             String left = S.substring(0,i);
  5.             String right = S.substring(i,S.length());
  6.  
  7.             System.out.print("On left:"+ numberChars(left,'('));
  8.             System.out.println("  .On right:"+ numberChars(right,')'));
  9.  
  10.             int openOnLeft= numberChars(left,'(');
  11.             int closedOnRight= numberChars(right,')');
  12.  
  13.             if((openOnLeft-closedOnRight)==0){
  14.                 System.out.println("Found on:"+i);
  15.                 return i;
  16.             }
  17.         }
  18.  
  19.         return 0;
  20.     }
  21.  
  22.     private static int numberChars(String str, char find){
  23.         int numFound=0;
  24.  
  25.         for(char c:str.toCharArray()){
  26.             if(c== find)
  27.             numFound++;
  28.         }
  29.        return numFound;
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement