This question already has an answer here:

Here's a scenario: A grandmother and grandfather forget what their GMail password is. They know the word, but can't remember which letters are capitals. The challenge is to take a word given and print(stdout) every combination of capital letters possible, essentially "brute-forcing" but only on a given string.


and would output



ea! would output



  • ASCII characters only
  • Input doesn't have to be lowercase
  • The order is not important
  • Non-alphabetic chars are left alone
  • Standard Loopholes apply.
  • Any form of input is allowed
  • Repetition is NOT allowed

Leaderboard Snippet:

function answersUrl(a){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+a+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(a,b){return"https://api.stackexchange.com/2.2/answers/"+b.join(";")+"/comments?page="+a+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(a){answers.push.apply(answers,a.items),answers_hash=[],answer_ids=[],a.items.forEach(function(a){a.comments=[];var b=+a.share_link.match(/\d+/);answer_ids.push(b),answers_hash[b]=a}),a.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(a){a.items.forEach(function(a){a.owner.user_id===OVERRIDE_USER&&answers_hash[a.post_id].comments.push(a)}),a.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(a){return a.owner.display_name}function process(){var a=[];answers.forEach(function(b){var c=b.body;b.comments.forEach(function(a){OVERRIDE_REG.test(a.body)&&(c="<h1>"+a.body.replace(OVERRIDE_REG,"")+"</h1>")});var d=c.match(SCORE_REG);d&&a.push({user:getAuthorName(b),size:+d[2],language:d[1],link:b.share_link})}),a.sort(function(a,b){var c=a.size,d=b.size;return c-d});var b={},c=1,d=null,e=1;a.forEach(function(a){a.size!=d&&(e=c),d=a.size,++c;var f=jQuery("#answer-template").html();f=f.replace("{{PLACE}}",e+".").replace("{{NAME}}",a.user).replace("{{LANGUAGE}}",a.language).replace("{{SIZE}}",a.size).replace("{{LINK}}",a.link),f=jQuery(f),jQuery("#answers").append(f);var g=a.language;/<a/.test(g)&&(g=jQuery(g).text()),b[g]=b[g]||{lang:a.language,user:a.user,size:a.size,link:a.link}});var f=[];for(var g in b)b.hasOwnProperty(g)&&f.push(b[g]);f.sort(function(a,b){return a.lang>b.lang?1:a.lang<b.lang?-1:0});for(var h=0;h<f.length;++h){var i=jQuery("#language-template").html(),g=f[h];i=i.replace("{{LANGUAGE}}",g.lang).replace("{{NAME}}",g.user).replace("{{SIZE}}",g.size).replace("{{LINK}}",g.link),i=jQuery(i),jQuery("#languages").append(i)}}var QUESTION_ID=110455,OVERRIDE_USER=8478,ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align: left!important; font-family: sans-serif}#answer-list,#language-list{padding: 10px; width: 290px; float: left}table thead{font-weight: 700}table td{padding: 5px; margin: 0}table tr:nth-child(even){background: #eee}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"><div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr> <td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table></div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr> <td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table></div><table style="display: none"> <tbody id="answer-template"> <tr> <td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody></table><table style="display: none"> <tbody id="language-template"> <tr> <td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody></table>


marked as duplicate by Poke, Mego code-golf Feb 18 '17 at 12:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 5
    \$\begingroup\$ You say print. Is it allowed to return the result from a function? That's usually permitted here. \$\endgroup\$ – Dennis Feb 18 '17 at 1:19
  • \$\begingroup\$ Also, do the output strings have to be unique or could we print all variations of ea! twice? \$\endgroup\$ – Dennis Feb 18 '17 at 1:26
  • 2
    \$\begingroup\$ Is the input guaranteed to be in lowercase? \$\endgroup\$ – user62131 Feb 18 '17 at 1:38
  • \$\begingroup\$ @JungHwanMin Sorry, I was just going to update to show the current winner \$\endgroup\$ – Geordie Fischer Feb 18 '17 at 1:55
  • 2
    \$\begingroup\$ I don't know what happened, but after outputting three combinations the program halted and I got an SMS, saying I had to stop... ? \$\endgroup\$ – Stewie Griffin Feb 18 '17 at 9:25

Jelly, 7 bytes


Try it online!

How it works

żŒsŒpQY Main link. Argument: s (string)

 Œs     Yield s with swapped case.
ż       Zip s with the result, creating an array of character pairs.
   Œp   Take the Cartesian product.
      Y Join, separating by linefeeds.
     Q  Unique; deduplicate the result.

Bash + GNU utils, 38

eval echo `sed 's/[a-z]/{\l&,\u&}/gi'`

Try it online.

The sed expression replaces every letter with {<LOWER>,<UPPER>}, where <LOWER> and <UPPER> are respectively the lower- and upper- case versions of that letter. These are bash brace expansions which are then expanded - the eval ensures this is the last expansion to happen.

  • \$\begingroup\$ That's clever, but it seems to break if the input contains certain special characters. Not sure if that's a problem. \$\endgroup\$ – Dennis Feb 18 '17 at 3:23
  • \$\begingroup\$ @DigitalTrauma Nicely done \$\endgroup\$ – Mitchell Spector Feb 18 '17 at 7:55
  • 1
    \$\begingroup\$ And the port to Perl (37 bytes) : Try it online! \$\endgroup\$ – Dada Feb 18 '17 at 10:26

MATL, 13 12 bytes


Try it online!

Same idea as Dennis, only less neat, with Luis Mendo's suggestion.


JavaScript (ES6), 99 97 bytes

Returns a space-separated list of words.

f=(s,i)=>--i<1?'':s.replace(/[a-z]/gi,c=>c[`to${i&n?'Upp':'Low'}erCase`](n*=2),n=1)+' '+f(s,i||n)

Test cases

f=(s,i)=>--i<1?'':s.replace(/[a-z]/gi,c=>c[`to${i&n?'Upp':'Low'}erCase`](n*=2),n=1)+' '+f(s,i||n)



PHP, 115 101 99 102 101 bytes

saved 14 bytes thanks @user63956.

function f($b,$a="
"){$b&a?f($c=substr($b,1),$a.$d=$b[0])|ctype_alpha($d^=" ")&&f($c,$a.$d):print$a;}

recursive function, prints the results with a leading newline


function f($b,$a="\n")                          // $b=remaining input, $a= prepared output
    // while $b is not empty
    $b&a        # this is not a typo!
        // recurse with $d(=first char of $b) appended to $a
        // if $d is letter, recurse with $d (case toggled) appended to $a
            |ctype_alpha($d)&&f($c,$a.$d^=" ")
    // if $b is empty, print $a
  • 1
    \$\begingroup\$ You can toggle the case of ASCII letters with ^' ' and get rid of *cfirst() calls. \$\endgroup\$ – user63956 Feb 18 '17 at 11:38
  • 1
    \$\begingroup\$ Checking if the string is not empty can be written as a&$b. \$\endgroup\$ – user63956 Feb 18 '17 at 12:47

Bash + Unix utilities, 95 bytes (non-recursive)

for((n=${#s};z<n*2**n;)){ echo -n "${s[z/n>>z%n&1]:z++%n:1}";}|sort -u

Try it online!

Note: This solution and the recursive solution below both require bash 4; the ${x,,} and ${x^^} constructs weren't available in bash 3.

This is quite a bit longer than @DigitalTrauma's solution, but I think it works on all strings, even if they have special characters in them. For example, with input


this program outputs


as it should. (@DigitalTrauma's solution, although very ingenious, doesn't work for this input and other similar inputs.)

Bash + Unix utilities, 88 bytes (recursive)

[ "$1" ]&&(c=${1:0:1};sed "h;s/^/${c,,}/;p;g;s/^/${c^^}/"<<<`$0 "${1:1}"`|sort -u)||echo

This recursive solution works on input {a,b} but it has problems with other special characters, for example, input a/b.


C 247 229 bytes

i,n,j,k,l;f(char *s){l=strlen(s);for(i=0;i<l;i++)s[i]=tolower(s[i]);int v[l];for(i=0;i<pow(2,l);i++){n=i,k=0;for(;n;k++){v[k]=n;n/=2;}for(j=0;j<l;j++){v[j]%=2;if(v[j])s[j]=toupper(s[j]);else s[j]=tolower(s[j]);}printf("%s ",s);}}

Ungolfed version:

void f(char *s)
  int i,num,k,l=strlen(s);
  for(int i=0;i<l;i++)

   int v[l];    


      for(int j=0;j<l;j++)

      printf("%s \n",s);       



  • Accept the character string, convert the string to lowercase.
  • Declare integer array of length equal to that of the string.
  • Store the numbers from 0 to 2^strlen(s)in binary form in an int array.( For a 3 byte string: 000,001,010...111)
  • Depending on if a bit at a position is set or, toggle the case.
  • Output the string for every possible combination.
  • \$\begingroup\$ Macroing strlen would save a bunch of bytes. Or better yet use while(c[i]) (you can assume that the strings are null terminated) \$\endgroup\$ – MrPaulch Feb 18 '17 at 11:53

JavaScript (Firefox 30-57), 92 bytes

f=([c,...a])=>c?[for(l of new Set([c.toLowerCase(),c.toUpperCase()]))for(r of f(a))l+r]:['']

Accepts a string or array of characters and returns an array of strings.

  • \$\begingroup\$ Now that this question has been marked duplicate, I've just discovered that I posted an eerily similar answer to that question... \$\endgroup\$ – Neil Feb 18 '17 at 19:15

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