Riddle225
BAN USERpublic class TwitterProblem {
public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
ArrayList<String> allStringsToManipulate = new ArrayList<String>();
String test1="(AB)C/";
String test2="(AB)C/S";
String test3="(AB)C/RS";
String test4="A(BC)/RS";
String test5="A(BC)/RSR";
String test6= "(AB)C((DE)F)A(BC)/RSR";
String test7= "(AB)C/S\nA(BC)/RSR";
//String test7= "(AB)C((DE)F)/SSSS";
allStringsToManipulate.add(test1);
allStringsToManipulate.add(test2);
allStringsToManipulate.add(test3);
allStringsToManipulate.add(test4);
allStringsToManipulate.add(test5);
allStringsToManipulate.add(test6);
String[] myList=allExpressionsInLine(test6);
for(String expression: myList){
String answer = stripEverythingAfterBackslash(expression);
char[] allOperations = operations(expression);
if(expression!=null || expression.length()>0){
expression=expression.trim();
expression = expression.replaceAll("\\s","");
boolean simplified=false;
int index = 0;
while (index < allOperations.length) {
if (allOperations[index] == 'S' &&!simplified|| allOperations[index] == 's' &&!simplified) {
String result = simplify(answer);
answer = result;
simplified=true;
} else if (allOperations[index] == 'R' || allOperations[index] == 'r') {
answer = reverse(answer);
} else {
}
index++;
}
System.out.println(answer);
}else{
System.out.println("");
}
}
}
static String[] allExpressionsInLine(String line){
String lines[] = line.split("(\r\n|\r|\n)", -1);
return lines;
}
static char[] operations(String expression){
int counter=0;
boolean begin =false;
for(int i=0;i<expression.length(); i++){
if(begin){
counter++;
}
if(expression.charAt(i)=='/'){
begin=true;
}
}
char[] allOperations = new char[counter];
begin=false;
int index=0;
for(int i=0;i<expression.length(); i++){
if(begin){
allOperations[index]= expression.charAt(i);
index++;
}
if(expression.charAt(i)=='/'){
begin=true;
}
}
return allOperations;
}
static String simplify(String expression){
String answer=expression;
int totalNests = isItWorthSimplifying(answer);
if(expression.charAt(0) =='(') {
answer= removeFirstPairOfParanthesis(expression);
}
if(totalNests>1) {
String result = removeNestedParanthesis(answer, totalNests);
return result;
}
return answer;
}
static int isItWorthSimplifying(String expression){
Stack<Character> openParan = new Stack<>();
int maxNest=0;
for(int i=0;i<expression.length();i++){
char currLetter= expression.charAt(i);
if(currLetter == '('){
openParan.push('(');
}else if(currLetter ==')'){
openParan.pop();
}else{
if(maxNest<openParan.size()){
maxNest=openParan.size();
}
}
}
return maxNest;
}
static String removeNestedParanthesis(String expression, int maxNest){
Stack<Character> openParan = new Stack<>();
StringBuilder word = new StringBuilder("");
int index=0;
while(index<expression.length()){
char currLetter= expression.charAt(index);
if(currLetter == '(' && openParan.size()+1<maxNest){
openParan.push('(');
word.append(currLetter);
}else if(currLetter ==')'){
if(openParan.size()!=maxNest){
word.append(currLetter);
}
openParan.pop();
}else{
if(currLetter == '(' && openParan.size()+1==maxNest){
openParan.push('(');
}else{
word.append(currLetter);
}
}
index++;
}
return word.toString();
}
static String removeFirstPairOfParanthesis(String s){
StringBuilder answer= new StringBuilder("");
boolean firstOpen=false;
boolean firstClosed=false;
for(int i=0; i<s.length(); i++){
if(s.charAt(0)=='(' && !firstOpen){
firstOpen=true;
}else if(s.charAt(i)== ')' && !firstClosed){
firstClosed=true;
}else{
answer.append(s.charAt(i));
}
}
return answer.toString();
}
static String stripEverythingAfterBackslash(String expression){
int end=expression.indexOf('/');
if(end!=-1) {
String newExpression = expression.substring(0, end);
return newExpression;
}
return expression;
}
static String reverse(String expression){
StringBuilder word = new StringBuilder("");
for(int i=expression.length()-1; i>=0; i--){
if(expression.charAt(i)=='(') {
word.append(")");
}else if(expression.charAt(i)==')'){
word.append("(");
}else{
word.append(expression.charAt(i));
}
}
return word.toString();
}
}
. lolol
- Riddle225 August 27, 2017
Couldnt think of a way to do it that wasn't lengthy. So Came up with what I think is an easy to read solution. No Idea why I was failing the last test case though. Feel Free to blow my mind with whats wrong with it
- Riddle225 August 27, 2017