0

I am kind of stuck. I don't want a code but kind of an idea on where to start or what to use in order to get kickstarted. I don't want a whole solution as it takes the fun away.

I know I would need a scanner to input a string/array/int with 4 digits which then should be swapped around in all possible options?

For loop for example swapping 1st number with second, third, fourth and then repeat while 1st number is last. But then how to avoid duplicates?

  • Are there any rules around input which has duplicates? You said that it cannot have duplicates, but you didn't specify what happens if the user inputs: "1222", which would only result in a few different unique strings. While it may not be very elegant, you could use a Set (HashSet, for example), and while you're looping you can add items to the Set. Once you've done all your permutations in your loops you can print out the results of the Set, knowing they'll be unique. It will not be efficient in terms of time/space complexity, but it will solve the problem. – Andrew Cotton Feb 13 '19 at 19:59
  • Forgot to mention I have started my java practice like 2-3 weeks ago so yeah :P I can't even seem to get the loop. – Patryk Sarnik Feb 13 '19 at 20:04
  • Just look up how to use for loops in java on the interwebs, and the learn how to loop over the characters in a string, so, something like this: https://stackoverflow.com/questions/196830/what-is-the-easiest-best-most-correct-way-to-iterate-through-the-characters-of-a Using these techniques, you can brute force the solution. There are many many ways to solve it, and some are more elegant than others, but getting the correct answer is most important, I imagine. – Andrew Cotton Feb 13 '19 at 20:16

0 Answers0