#Mathematica 126
Mathematica 126
With x
holding the input string,
t = ToExpression;
Length@ConnectedComponents@Graph[Characters /@ ImportString[x, "Table"][[1]] /.
{a_, "-", b_} :> UndirectedEdge[t@a, t@b]]
If x
is input instead as a list of undirected edges, in standard Mathematica format, the solution can be reduced to 34 chars:
Length@ConnectedComponents@Graph@i
Example: