-3

I have this sample string "AHHHAAAAAARTFUHLAAAAAHV" and i want to minimized it to "AHHHAARTFUHLAHV". Notice that the first occurrence of successive A's is 6 and the next is 5. So I can't use:

  System.out.println("AHHHAAAAAARTFUHLAAAAAHV".replaceAll("A{4}(?!A)", ""));

because of the irregular size of A's is there anyway around to make it "AHHHAARTFUHLAHV"?

  • 1
    Can you explain more about what you mean by minimizing? what is the rule here? – Peyman Mahdian Mar 22 '17 at 21:19
  • @PeymanMahdian Im sorry for the term "minimizing". I simply mean to remove the extra characters to achieve my desired output which is "AHHHAARTFUHLAHV – Flor Anthony De La Pena Mar 22 '17 at 21:22
  • `System.out.println("AHHHAAAAAARTFUHLAAAAAHV".replaceAll("A{6}", "AA").replaceAll("A{5}", "A"));` – Elliott Frisch Mar 22 '17 at 21:27
  • @ElliottFrisch thank you for your reply but im kinda getting this error "error: unmappable character for encoding MS932" – Flor Anthony De La Pena Mar 22 '17 at 21:31
  • @FlorAnthonyDeLaPena Weird. I tested it. It produces your requested output (albeit in a hacky way). – Elliott Frisch Mar 22 '17 at 21:33
  • I'm confused, the output you want appears to be exactly what you get: `assertThat("AHHHAAAAAARTFUHLAAAAAHV".replaceAll("A{4}(?!A)", ""), equalTo("AHHHAARTFUHLAHV"));`. – James Mar 22 '17 at 21:41
  • but you are not just removing the all extra characters in your example. you have AA? – Peyman Mahdian Mar 22 '17 at 21:41
  • It is not clear from your question what you are trying to achieve. If your input String will always be as in the example the the answer is trivial, so there must be more to it. There must be a rule that means the 6 As get replaced with two As, and the 5 As get replaced with only one. But what is the rule? What would be the minimised string if there were 7 As? Or if the 5 As came before the 6 As? Could that even happen? – Kevin Sadler Mar 22 '17 at 21:42
  • Is the error you encountered related to this problem: http://stackoverflow.com/a/4996583/7421645 – James Mar 22 '17 at 21:46

1 Answers1

0

You could try to manipulate the string. Like look for the char A and if it appears more than x times consecutively you can just eliminate the other ones. I hope this helps somehow