• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String permutation

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I need to replace certain text with permutation.

E.g.: the word AEMERAE should converted to ÄMERÄ, ÄMERAE, AEMERÄ or
the word AEMERAEOE should converted to ÄMERÄÖ, ÄMERAEOE, AEMERÄOE, AEMERÄOE etc.

With command "replace" I've tried to replace the word but it is done all at once. How can I make it with permutation?



Thank you for any help.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tom nasil wrote:Hello,

I need to replace certain text with permutation.

E.g.: the word AEMERAE should converted to ÄMERÄ, ÄMERAE, AEMERÄ or
the word AEMERAEOE should converted to ÄMERÄÖ, ÄMERAEOE, AEMERÄOE, AEMERÄOE etc.

With command "replace" I've tried to replace the word but it is done all at once. How can I make it with permutation?



Thank you for any help.




If by permutation, you mean every possible option of replacement -- then obviously, there is no single replace() method call that will work, as you need to keep multiple results with different variations of what has been replaced and what has not.

I would recommend finding everything that can change, and then change copies of the original, either with nested loops, or my favorite, with recursion.

Henry
 
Master Rancher
Posts: 4806
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would pursue a recursive strategy. Try making a method like this:

The prefix would be a string that does not change, to be prepended to each alternate, while the input is what makeAlternates is responsible for making alternate versions of. Within makeAlternates, you just need to find the first thing that has alternate versions (I.e. Ä, Ö, AE, OE, and whatever else has alternate forms. Then recursively call makeAlternates for the remaining things to be found.

So given

FOOÄBARÖBAZ

if you call

makeAlternates("", "FOOÄBARÖBAZ");

it should find that Ä is the first thing it needs to alternate on, so it should then call

makeAlternates("FOOÄ", "BARÖBAZ");

and then

makeAlternates("FOOAE, "BARÖBAZ");


Expanding on that, you would get the following sequence of calls:



 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic