FastPrepReplace Characters in a Mutable String

Replace Characters in a Mutable String

Bloomberg LP logoBloomberg LP● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

Replace every occurrence of the one-character string from in text with the one-character string to. Return the result.

Function

replaceCharacter(text: String, sourceCharacter: String, targetCharacter: String) → String

Examples

Example 1

text = "banana"sourceCharacter = "a"targetCharacter = "o"return = "bonono"

All three a characters become o.

Constraints

  • 0 <= text.length <= 10^6.
  • from.length == to.length == 1.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String replaceCharacter(String text, String sourceCharacter, String targetCharacter) {
  // Write your code here.
}
text"banana"
sourceCharacter"a"
targetCharacter"o"
expected"bonono"
Checking account…