Let $F_2=\langle a, b\rangle$ be the non-abelian free group with two generators and $e$ is the neutral element in $F_2$.
Given $g\in F_2, k\geq 2$ an integer.
I want to know how to solve the word problem $g^{n_1}h^{m_1}\cdots g^{n_k}h^{m_k}=e$ with $\sum_{i}(n_i+m_i)=0$ for $h$.
I tend to guess that the only solution is there exists some $s\in F_2$ such that $g, h\in\langle s\rangle$.
For the special case $g=a, k=2$, it should be a direct calculation, but I only tried some simple cases..
Any reference or suggestion for this question?
There are exponents such that any $h$ satisfies your equation.
On the other hand, if $\langle g,h\rangle$ is not cyclic, then $h$ cannot be a solution when all the exponents are nonzero. The fact you seem to be missing is the Nielsen-Schreier theorem which states that subgroups of free groups are free, so $g$ and $h$ are free generators of the subgroup they generate unless that subgroup is already generated by one element.