Another Way To Write the IsIsogram Function

Hello! Before watching the two videos regarding checking characters, I worked out a solution that does work. The difference is that every character is checked against every character including itself. So obviously, it is less efficient for the purposes of this game. I was just wondering if anyone knew of an instance where this method might be useful.

bool UBullCowCartridge::IsIsogram(FString Word) const
{
    for (int32 i = 0; i < Word.Len(); i++)
    {
        for (int32 j = 0; j < Word.Len(); j++)
        {
            if (Word[i] == Word[j] && i != j)
            {
                return false;
            }
            
        }    
    }
    return true;
}

I ignored the times where the function checks the same index by adding it as a condition in my if statement. So does anyone know when this style might be useful?

It depends!

for checking isograms? Not so much.

For some other applications? you’re definitely stuck into computing pair-wise everything!

An example is a molecular dynamics simulation: every molecule has an electrodynamic force applied to every other molecule. You don’t really have a choice but to compute the O(n^2) forces similar to your nested loops. Sometimes you can get away with nifty domain specific approximations but sometimes you’re just stuck with having to grunt through it (which is where super computers come in if you have one!).

1 Like