Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

recently I have been given a task of finding the combinations of the given word. For example, say The given word is like then possible answers are liek,lkie and so on. This has to be implemented in any language, doesn't matter, but the rule is we don't use the built-functions/API to find the combinations. And I guess we know design patterns are for solving any problem with a given solution. For example, Information Expert is a Design Pattern. Now my question is whether there is any design pattern for finding combinations? Or for these kind of things Design patterns wont suit and say i have to learn a good algorithm for finding the solution. Then what's the difference between Algorithm and Patterns, both try to solve the problem in a good efficient way, right?

I don't know wthether my view is correct or not, please correct if I am wrong!

share|improve this question
Are you just making random changes to the string? Finding all the possible combinations of the letters? The problem is not very well specified, IMO... –  Dean Harding Mar 20 '11 at 11:11
I'm just swapping the first two letters and check in the answer list(which hold answers) for duplicates, if they found already then i will skip,then i will swap the next second and third letter and keep on goes untill i found the answer. The loop will get exit if it done all the combinations, that is before entering the loop i will found the number of possible of combinations which then act as upper bound for my loop! –  Ant's Mar 20 '11 at 11:20
That sounds.... expensive! –  Kenneth Mar 20 '11 at 15:28
The number of combinations is n! for a n letter word assuming all letters are unique. Handling duplicate letters could be a major pain with this kind of problem but this does seem like a common Combinatorics problem to my mind. –  JB King Mar 24 '11 at 22:13
You say you're looking for combinations, but your example is of finding permutations. Also, the number of combinations for a string of length n containing no duplicate letters is not n!; the number of permutations is. –  user16764 Apr 21 '11 at 16:22

1 Answer 1

up vote 14 down vote accepted

An algorithm is an effective method expressed as a finite list of well-defined instructions for calculating a function. A design pattern is a general reusable solution to a commonly occurring problem in software design.

You have a function you need to calculate (the combinations of letters from a list of letters), which means you definitely need an algorithm. Once you have an algorithm, you may find that you can implement it using a reusable solution, in which case you may be able to employ a design pattern. However you cannot apply a reusable solution before you know what the solution ought to look like, so solve your problem first.

share|improve this answer
+1 for However you cannot apply a reusable solution before you know what the solution ought to look like, so solve your problem first. –  Sandeepan Nath Apr 21 '11 at 15:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.