bool UBullCowCartridge::IsIsogram(FString Word) const
{
std::vector<bool> Letters(26, false);
for (auto&& Char : Word)
{
const int Index = Char - 'A';
if (Letters[Index])
{
return false;
}
Letters[Index] = true;
}
return true;
}
Assuming you have sanitized the Guess / Word (only kept characters A to Z) and sent it as uppercase (Guess.ToUpper()), this would be an efficient solution.
- Declare a placeholder for each possible character (26) and initialize them all to false (as if none of those letters have been seen by default).
- Loop each character of the word.
- Get the corresponding index in the placeholder by substracting the char with the first possible character (A). e.g. A - A = Index 0, B - A = Index 1, etc.
- If the value at position Letters[Index] is true, that means we’ve already seen that letter, so it’s not an Isogram and we can get out immediately.
- Otherwise, set the position of Letters[Index] to true to indicate that we’ve seen that letter (the next time we’ll check that position (step #4) we’ll know we’ve seen it already).
- We’ve looped all characters of the word, all of them were unique (never returned with false from #4) so it is in fact an isogram, return true.