<p>Suppose I have:</p>

<ol>
<li>Toby</li>
<li>Tiny</li>
<li>Tory</li>
<li>Tily</li>
</ol>

<p>Is there an algorithm that can easily create a list of common characters in the same positions in all these strings?  (in this case the common characters are 'T' at position 0 and 'y' at position 3)</p>

<p>I tried looking at some of the algorithms used for DNA sequence matching but it seems most of them are just used for finding common substrings regardless of their positions.</p>

[{'answer_id': 68701, 'author': 'Brian R. Bondy', 'author_id': 3153, 'author_profile': 'https://Stackoverflow.com/users/3153', 'pm_score': 3, 'selected': True, 'text': "<p>Finding a list of characters that are common in ALL strings at a certain position is trivially simple.  Just iterate on each string for each character position 1 character position at a time.  If any string's character is not the match of it's closest neighbor string's character, then the position does not contain a common character.</p>\n\n<p>For any i = 0 to length -1... Once you find Si[x] != Si+1[x] you can skip to the next position x+1.</p>\n\n<p>Where Si is the ith string in the list.  And [x] is the character at position x.</p>\n"}, {'answer_id': 68752, 'author': 'Josh Smeaton', 'author_id': 10583, 'author_profile': 'https://Stackoverflow.com/users/10583', 'pm_score': 1, 'selected': False, 'text': '<p>Some generic code that has pretty poor performance O(n^2)</p>\n\n<pre><code>str[] = { "Toby", "Tiny", "Tory", "Tily" };\nresult = null;\nlargestString = str.getLargestString(); // Made up function\nstr.remove(largestString)\nfor (i = 0; i &lt; largestString.length; i++) {\n   hits = 0;\n   foreach (str as value) {\n      if (i &lt; value.length) {\n         if (value.charAt(i) == largestString.charAt(i))\n            hits++;\n      }\n   }\n   if (hits == str.length)\n      result += largestString.charAt(i);\n}\nprint(str.items);\n</code></pre>\n'}, {'answer_id': 68780, 'author': 'theo', 'author_id': 7870, 'author_profile': 'https://Stackoverflow.com/users/7870', 'pm_score': 1, 'selected': False, 'text': '<p>I can\'t think of anything especially optimized.</p>\n\n<p>You can do something like this, which shouldn\'t be too hard:</p>\n\n<pre><code>        //c# -- assuming your strings are in a List&lt;string&gt; named Names\n        int shortestLength = Names[0].Length, j;\n        char[] CommonCharacters;\n        char single;\n\n        for (int i = 1; i &lt; Names.Count; i++)\n        {\n            if (Names[i].Length &lt; shortestLength) shortestLength = Names[i].Length;\n        }\n\n        CommonCharacters = new char[shortestLength];\n        for (int i = 0; i &lt; shortestLength; i++)\n        {\n            j = 1;\n            single = Names[0][i];\n            CommonCharacters[i] = single;\n            while (j &lt; shortestLength)\n            {\n                if (single != Names[j][i])\n                {\n                    CommonCharacters[i] = " "[0];\n                    break;\n                }\n                j++;\n            }\n        }\n</code></pre>\n\n<p>This would give you an array of characters that are the same across everything in the list.</p>\n'}, {'answer_id': 68813, 'author': 'hoyhoy', 'author_id': 3499, 'author_profile': 'https://Stackoverflow.com/users/3499', 'pm_score': 1, 'selected': False, 'text': '<p>What about something like this?</p>\n\n<pre><code>strings = %w(Tony Tiny Tory Tily)\npositions = Hash.new { |h,k| h[k] = Hash.new { |h,k| h[k] = 0 } }\nstrings.each { |str| \n  0.upto(str.length-1) { |i| \n    positions[i][str[i,1]]+=1 \n  }\n}\n</code></pre>\n\n<p>At the end of execution, the result will be:</p>\n\n<pre><code>positions = {\n  0=&gt;{"T"=&gt;4},\n  1=&gt;{"o"=&gt;2, "i"=&gt;2}, \n  2=&gt;{"l"=&gt;1, "n"=&gt;2, "r"=&gt;1},\n  3=&gt;{"y"=&gt;4}\n}\n</code></pre>\n'}, {'answer_id': 68877, 'author': 'Todd Gamblin', 'author_id': 9122, 'author_profile': 'https://Stackoverflow.com/users/9122', 'pm_score': 1, 'selected': False, 'text': '<p>Here\'s an algorithm in 5 lines of ruby:</p>\n\n<pre><code>#!/usr/bin/env ruby\nchars = STDIN.gets.chomp.split("")\nSTDIN.each do |string|\n  chars = string.chomp.split("").zip(chars).map {|x,y| x == y ? x : nil }\nend\nchars.each_index {|i| puts "#{chars[i]}  #{i}" if chars[i] }\n</code></pre>\n\n<p>Put this in commonletters.rb. Sample usage:</p>\n\n<pre><code>$ commonletters.rb &lt; input.txt\nT  0\ny  3\n</code></pre>\n\n<p>Assuming that input.txt contains:</p>\n\n<pre><code>Toby\nTiny\nTory\nTily\n</code></pre>\n\n<p>This should work with whatever inputs you throw at it. It will break if the input file is empty, but you can probably fix that yourself.  This is O(n) (n is total number of chars in the input).</p>\n'}, {'answer_id': 103810, 'author': 'Nick Johnson', 'author_id': 12030, 'author_profile': 'https://Stackoverflow.com/users/12030', 'pm_score': 1, 'selected': False, 'text': "<p>And here's a trivial version in Python:</p>\n\n<pre><code>items = ['Toby', 'Tiny', 'Tory', 'Tily']\ntuples = sorted(x for item in items for x in enumerate(item))\nprint [x[0] for x in itertools.groupby(tuples) if len(list(x[1])) == len(items)]\n</code></pre>\n\n<p>Which prints:</p>\n\n<pre><code>[(0, 'T'), (3, 'y')]\n</code></pre>\n\n<p>Edit: Here's a better version that doesn't require creating a (potentially) huge list of tuples:</p>\n\n<pre><code>items = ['Toby', 'Tiny', 'Tory', 'Tily']\nminlen = min(len(x) for x in items)\nprint [(i, items[0][i]) for i in range(minlen) if all(x[i] == items[0][i] for x in items)]\n</code></pre>\n"}, {'answer_id': 104370, 'author': 'Morikal', 'author_id': 18272, 'author_profile': 'https://Stackoverflow.com/users/18272', 'pm_score': 0, 'selected': False, 'text': '<p>In lisp:</p>\n\n<pre><code>CL-USER&gt; (defun common-chars (&amp;rest strings)\n           (apply #\'map \'list #\'char= strings))\nCOMMON-CHARS\n</code></pre>\n\n<p>Just pass in the strings:</p>\n\n<pre><code>CL-USER&gt; (common-chars "Toby" "Tiny" "Tory" "Tily")\n(T NIL NIL T)\n</code></pre>\n\n<p>If you want the characters themselves:</p>\n\n<pre><code>CL-USER&gt; (defun common-chars2 (&amp;rest strings)\n           (apply #\'map\n                  \'list\n                  #\'(lambda (&amp;rest chars)\n                      (when (apply #\'char= chars)\n                        (first chars))) ; return the char instead of T\n                  strings))\nCOMMON-CHARS2\n\nCL-USER&gt; (common-chars2 "Toby" "Tiny" "Tory" "Tily")\n(#\\T NIL NIL #\\y)\n</code></pre>\n\n<p>If you don\'t care about posiitons, and just want a list of the common characters:</p>\n\n<pre><code>CL-USER&gt; (format t "~{~@[~A ~]~}" (common-chars2 "Toby" "Tiny" "Tory" "Tily"))\nT y \nNIL\n</code></pre>\n\n<p>I admit this wasn\'t an algorithm... just a way to do it in lisp using existing functionality</p>\n\n<p>If you wanted to do it manually, as has been said, you loop comparing all the characters at a given index to each other. If they all match, save the matching character.</p>\n'}, {'answer_id': 111699, 'author': 'EvilTeach', 'author_id': 7734, 'author_profile': 'https://Stackoverflow.com/users/7734', 'pm_score': 1, 'selected': False, 'text': '<pre><code>#include &lt;iostream&gt;\n\nint main(void)\n{\n    char words[4][5] = \n    {\n        "Toby",\n        "Tiny",\n        "Tory",\n        "Tily"\n    };\n\n    int wordsCount = 4;\n    int lettersPerWord = 4;\n\n    int z;\n    for (z = 1; z &lt; wordsCount; z++)\n    {\n        int y;\n        for (y = 0; y &lt; lettersPerWord; y++)\n        {\n            if (words[0][y] != words[z][y])\n            {\n                words[0][y] = \' \';\n            }\n        }\n    }\n\n    std::cout &lt;&lt; words[0] &lt;&lt; std::endl;\n\n    return 0;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/68664', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9825/']