This is a problem that doesn't have an easy solution. You might take a look at this thread in which some of the Tableau Zen Masters discuss different approaches and the pros and cons of each.
I've attached a workbook that gives a possible solution -- and although it works for the simple data set you provided (note that I added a row to give me something for the "both" column), it is just a starting point and the ultimate solution is likely more complex. Ultimately, with a large or complex initial data set, you'd probably want to shape your data on the front end prior to connecting Tableau.
Distinct.twbx.zip 15.8 KB