remote87

reverseInParentheses

Apr 11th, 2021 (edited)
422
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. String reverseInParentheses(String inputString) {
  2.    
  3.     if(inputString.isEmpty() || inputString.equals("()")) return "";
  4.    
  5.     StringBuilder temp = new StringBuilder();
  6.     Pattern pattern = Pattern.compile("[(]([\\w]+)[)]");
  7.     Matcher matcher = pattern.matcher(inputString);
  8.        
  9.     while(inputString.contains("(") && inputString.contains(")")){    
  10.         if(matcher.find()){
  11.             temp = new StringBuilder(matcher.group(1));
  12.             temp.reverse();
  13.             inputString = inputString.replace(matcher.group(0), temp);
  14.             matcher = pattern.matcher(inputString);
  15.         } else {
  16.             return inputString;
  17.         }
  18.     }
  19.     return inputString;
  20. }
  21.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×