a combination of string manipulation and recursive problem