I have a list of graph edges. E.g.

{"woods" <-> "woody", "aback" <-> "alack", "tummy" <-> "yummy", "tumor" <-> "tutor"}

How can I compare the vertices to extract the same letters?

Coming from another programming language, I know how to do this using a nested loop and by comparing the characters, but this seems remarkably inappropriate.

  • $\begingroup$ What do you mean by extract the same letter? Can you give us a desired output? $\endgroup$
    – Wjx
    Aug 17, 2016 at 6:04
  • $\begingroup$ Please describe more precisely how comparing should be done, what is the expected result and assumed input (sam lenght? etc) $\endgroup$
    – Kuba
    Aug 17, 2016 at 6:05
  • $\begingroup$ @Wjx, desired output for the following example is {"wood", "aack", "ummy", "tuor"} $\endgroup$
    – eliott
    Aug 17, 2016 at 6:07

1 Answer 1


One liner:

LongestCommonSequence @@@ {"woods" <-> "woody", "aback" <-> "alack", "tummy" <-> "yummy", "tumor" <-> "tutor"}

{"wood", "aack", "ummy", "tuor"}

LongestCommonSubsequence extract things for each pair and @@@ apply this on level 1.

UndirectedEdge @@ Transpose@Cases[SequenceAlignment[##], _List] & @@@ {"woods" <-> "woody", "aback" <-> "alack", "tummy" <-> "yummy", "tumor" <-> "tutor"}

{{"s"} <-> {"y"}, {"b"} <-> {"l"}, {"t"} <-> {"y"}, {"m"} <-> {"t"}}


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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