We describe an optimal algorithm to decide if one closed curve on a triangulated 2-manifold can be continuously transformed to another, i.e., if they are homotopic. SupposeC 1 andC 2 are two closed curves on a surfaceMof genusg. Further, supposeTis a triangulation ofMof sizensuch thatC 1 andC 2 are represented as edge–vertex sequences of lengthsk 1 andk 2 inT, respectively. Then, our algorithm decides ifC 1 andC 2 are homotopic inO(n+k 1 +k 2 ) time and space, providedg≠2 ifMis orientable, andg≠3, 4 ifMis nonorientable. This implies as well an optimal algorithm to decide if a closed curve on a surface can be continuously contracted to a point. Except for three low genus cases, our algorithm completes an investigation into the computational complexity of two classical problems for surfaces posed by the mathematician Max Dehn at the beginning of this century. The novelty of our approach is in the application of methods from modern combinatorial group theory.