Skip to content

Instantly share code, notes, and snippets.

@liron-navon
Created September 16, 2021 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liron-navon/2e6a05aa80a6121c23dbb6a832eb15a8 to your computer and use it in GitHub Desktop.
Save liron-navon/2e6a05aa80a6121c23dbb6a832eb15a8 to your computer and use it in GitHub Desktop.
Solution for - CodeSignal - Reverse In Parentheses
// 1. Find the pairs of parentheses
function getStartAndEndIndexes(inputString) {
const regularExpression = /\([a-zA-Z]*?\)/;
const execData = regularExpression.exec(inputString);
if(!execData) {
return null;
}
const startIndex = execData.index;
const endIndex = execData.index + execData[0].length - 1;
return {startIndex, endIndex}
}
// 2. Find the part of the string we need to reverse
function reverseParentheses(startIndex, endIndex, inputString) {
// before the parenthesis
const startSegmant = inputString.substring(0, startIndex);
// the parenthesis
const parenthesisSegmant =
inputString.substring(startIndex +1, endIndex);
// after the parenthesis
const endSegmant =
inputString.substring(endIndex + 1, inputString.length);
return startSegmant + reverse(parenthesisSegmant) + endSegmant;
}
// 3. Reverse the string
function reverse(string) {
return string.split('').reverse().join('');
}
// 4. Repeat until all parentheses are replaced
function reverseInParentheses(inputString) {
let indexes = getStartAndEndIndexes(inputString);
while(indexes) {
const {startIndex, endIndex} = indexes;
const newString =
reverseParentheses(startIndex, endIndex, inputString);
inputString = reverseInParentheses(newString);
indexes = getStartAndEndIndexes(inputString);
}
return inputString;
}
@mahmed1987
Copy link

Solution For Kotlin

fun solution(inputString: String): String {
  val list = startAndEndIndices(inputString)
    var outputString = inputString
    for (pair in list) {
      val wordToReverse = outputString.substring(pair.first + 1, pair.second)
      outputString =
        outputString.replaceRange(pair.first + 1 until pair.second, wordToReverse.reversed())
    }
    return outputString.replace("(", "").replace(")", "")
}


private fun startAndEndIndices(inputString: String): List<Pair<Int, Int>> {
    val stackOfLeftBraces: Stack<Int> = Stack()
    val startEndIndexPair = mutableListOf<Pair<Int, Int>>()
    for (i in inputString.indices) {
      if (inputString[i] == '(') {
        stackOfLeftBraces.push(i)
      } else if (inputString[i] == ')') {
        startEndIndexPair.add(stackOfLeftBraces.pop() to i)
      }
    }
    return startEndIndexPair
  }

@vetaprog85samir
Copy link

Solution For Kotlin

fun solution(inputString: String): String {
  val list = startAndEndIndices(inputString)
    var outputString = inputString
    for (pair in list) {
      val wordToReverse = outputString.substring(pair.first + 1, pair.second)
      outputString =
        outputString.replaceRange(pair.first + 1 until pair.second, wordToReverse.reversed())
    }
    return outputString.replace("(", "").replace(")", "")
}


private fun startAndEndIndices(inputString: String): List<Pair<Int, Int>> {
    val stackOfLeftBraces: Stack<Int> = Stack()
    val startEndIndexPair = mutableListOf<Pair<Int, Int>>()
    for (i in inputString.indices) {
      if (inputString[i] == '(') {
        stackOfLeftBraces.push(i)
      } else if (inputString[i] == ')') {
        startEndIndexPair.add(stackOfLeftBraces.pop() to i)
      }
    }
    return startEndIndexPair
  }

thank you @mahmed1987

@mdavit
Copy link

mdavit commented Feb 7, 2023

Solution for Swift

func solution(inputString: String) -> String {
var result = ""
var stack = String
for letter in inputString {
if letter == "(" {
stack.append("")
} else if letter == ")" {
guard let last = stack.popLast() else { continue }
let reversedString = String(last.reversed())
if let lastStack = stack.last {
stack[stack.count - 1] = lastStack + reversedString
} else {
result += reversedString
}
} else if let lastStack = stack.last {
stack[stack.count - 1] += String(letter)
} else {
result += String(letter)
}
}
return result
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment