I am trying to generate a nucleotide motif that will code chosen amino acids. For example - histidine is coded by CAT, CAC. Arginine is CGT, CGC, CGA, CGG,AGA and AGG. The pattern is:

position in codon - C or A
position in codon - A or G
position - A, T, C or G

by that rule you can define chosen amino acids (H and R) but also the amino acids that i dont want (for example AAA is lysine, AAT is asparagine...). So I need to define the pattern that matches only my chosen AAs, in case above it can be: [C][A or G][T], that pattern defines only histidine and arginine, but not the other amino acids. I am trying to work out an algorithm which will do this thing with any amino acids which i choose (more than two) and if the pattern does not exist it should find the possibilities for less amino acids (for example if pattern for 5 amino acids does not exist, it will find the patterns for four amino acids from the query) - this final optimization problem is probably the hardest part. Any suggestions? Thanks a lot and sorry for my poor english.
The problem was solved:
1) I made a library of all codons from the aminoacids of choise (ex. Met and Trp are AUG and UGG - so library of all combinations consists of [A/U][U/G][G] - AUG,AGG,UUG,UGG
2) Created two lists - first of "good" aminoacids - AUG, UGG and second of "bad" - AGG, UUG
3) Calculated the amount of "good" aminoacids left, if I remove a specific nucleotide (say if I remove U in second position I lose my AUG for methionine - so for U in second position the number is 1) for each nucleotide in each position
4) Next "bad" codons were analyzed for occurence of each nucleotide in each position (for codons AGG and UUG I have 2x G in third, 1x G in second, 1x U in first etc.)
5) After these steps I simply took the highest number from the step 4), looked to the list in step 3) and if the G in third position can be harmlessly removed without any losses in good aminoacids (not possible in our example, but can be done in larger codon sets) - I remove all codons with G in third position, edit the list of "good" and "bad" codons, and proceed again to step 3)
Bioinformatics - common motif for amino acids