The cycle is pretty simple, and I'm not sure what you can do much better. You may be able to optimize the order of the expression in the if statement. For example, due to the && short circuit, it might be faster to arrange the if statement this way
if (mask[index]!= word[index])) && (mask[index] != ' ')
Assuming matching characters are more likely to match the pattern. Of course, this is just a theory, I would not believe that this changed the situation without comparing it.
And as others pointed out, the subroutine crashes if the mask and line do not have the same length.
source share