IsIsogram()... why do we need to check for 0 and 1-length words?

Here’s my solution:

bool FBullCowGame::IsIsogram(FString Word) const
{
TMap<char, bool> LetterSeen;

for (auto Letter : Word) // for all letters of the word
{
Letter = tolower(Letter); // convert all letters to lower case
if (LetterSeen[Letter]) { // if the letter is in the map
// It’s not an isogram
return false;
}
else {
// Update the map to say the letter has been seen
LetterSeen[Letter] = true;
}
}
return true;
}

I don’t see the need to check for 0- and 1-character strings. When those inputs come through, they’ll just fall through the loop and return true, which is the desired result. Is there something I’ve missed?

I wondered the same. My guess was that the range based for loop would crash if it had a zero character string to deal with, but it seems to work fine.

The for auto loop won’t crash with 0, it just won’t go in the loop

Privacy & Terms