My implementation of IsIsogram function using while loops

This is my IsIsogram function implementation. I used nested while loops here. Just put down in codes whatever that came through my mind. I have no idea which one is better: for or while loop, just went for the latter.

bool UBullCowCartridge::IsIsogram(FString Word)
{
    int32 counter=1;
    int32 counter2=0;

    while(counter < Word.Len())
    {
        while(counter2 < counter)
        {
            if(Word[counter] == Word[counter2])
            {
                return false;
            }
            ++counter2;
        }
        counter2 = 0; //Reset counter2
        ++counter; //Increment counter
    }
    return true;
}

Output:

1 Like

Experimenting is the key to learning on your own. Iā€™m proud of you for testing out what you think is the best way

1 Like

Thanks for sharing!
I tried approaching the FString as a collection of TCHARs (it works, but is NOT efficient)

bool UBullCowCartridge::IsIsogram(const FString& Word)
{
	int32 TotalMatches;

	for (auto& Letter1 : Word)
	{
		TotalMatches = 0;
		for (auto& Letter2 : Word)
		{
			if (Letter1 == Letter2) ++TotalMatches;
			if (TotalMatches > 1) return false;
		}
	}
	return true;
}

@dimi_yumnam, with your inpiration of efficiency, I refactored it :nerd_face:

bool UBullCowCartridge::IsIsogram(const FString& Word) const
{
	const int32 WordLength = Word.Len();

	for (int32 I = 0; I < WordLength - 1; I++)
	{
		for (int32 J = I + 1; J < WordLength; J++)
		{
			if (Word[I] == Word[J]) return false;;
		}
	}
	return true;
}
2 Likes

Thanks. Appreciate it :slightly_smiling_face:

Good work :+1:

Privacy & Terms