Why does IsIsgram() work the way it does?

I feel like I’m missing something obvious, but right now I don’t get why our function IsIsogram() is returning “true” when the word is an isogram. I’m wondering because we didn’t tell the function to return “true” in the case that we got a word with more than one letter that is an isogram., we just told it to return “true” in case the word is 0 or 1 letters long.

We DO return ‘true’, at the bottom of the function, after the ‘for’ loop.

bool FBullCowGame::IsIsogram(FString inputIsogram) const
{
	if (inputIsogram.length() <= 1) { return true; };

	TMap<char, bool> LetterSeen;
	// loop through all the letters of the word
	for (auto Letter : inputIsogram)
	{
		Letter = tolower(Letter);

		if (LetterSeen[Letter] == true) { return false; }
		else { LetterSeen[Letter] = true; };

	};
};

I found this confusing as well; at first.

I had to go through it with the debugger step by step to see what was going on. To save you the time:

The if statement says
“If what is in the Map (letterseen) is true- return false”
Letterseen1 !

As you can see; this was the first step in the loop. The Map automagically adds the new character - ‘104’ here. When added; the Tmap auto initialies the value as 'False."

Letterseenp2 !

As we get to the second letter (Which somehow was allotted to ‘101’ - that I don’t understand quite yet.) You can see that it ran the ‘else’ statement. Which changed the last letter we saw to “True.”

Letterseenp3

This will continue until all letters are set to true; or until the letter seen runs the ‘IF’ statement- return a false boolean for the function.

Hope this helps!

1 Like

Privacy & Terms