title: Java: replacement in a string according to multiple rules
body: <p>I have the following task. There is a string. I must do replacement in it according to 6 rules till it is possible to make a replacement in a string. </p>
  
  <p>The solution I found is below. It works right. The problem is that its performance is low. How else can I make replacement according to multiple rules? Is there any algorithm?</p>
  
  <p>P.S. This task is from codility site. I got 100% correctness and 25% performance for my solution. </p>
  
  <pre><code>class Test {
  
      private Map&lt;String,String&gt; rules;
  
      private void initRules(){
          rules=new HashMap&lt;&gt;();
          rules.put("AB", "AA");
          rules.put("BA", "AA");
          rules.put("CB", "CC");
          rules.put("BC", "CC");
          rules.put("AA", "A");
          rules.put("CC", "C");
      }
  
      public String test(String S) {
          initRules();
          loop:while(true){
              String oldString=S;
              for (Map.Entry&lt;String, String&gt; rule : rules.entrySet())
              {
                  S=S.replace(rule.getKey(), rule.getValue());
              }
              if (oldString==S){
                  break loop;
              };
          }
          return S;
      }
  }
  
   public class NewMain {
  
      public static void main(String[] args) {
          Test test=new Test();
          System.out.println("Result:"+test.test("ABBCC"));;
      }
  
  }
  </code></pre>