Two Ways for IsIsogram() Implementation

When researching how to use the Map container, I ended up finding two ways that seem to work as expected; though I’m not sure which one is actually the fastest. Are there any dangers or foreseen problems with doing one of these?

  1. Try to find the letter in the Map container, if it’s not there add the letter to the Map; else return false (not an isogram).
  2. The Map will not duplicate entries so add all letters to the Map the compare the map.size vs the word.length. (This one is commented out in the below code.)

bool FBullCowGame::IsIsogram(FString Word) const
{
//Tread 0 and 1 letter words as isograms
if (Word.length() <= 1) { return true; }

//Setup a map
TMap<char, bool> LetterSeen; 

for (auto Letter : Word) //For all letters of the word
{
	Letter = tolower(Letter); //Handle mixed capitalization case
	
	//If the letter can be found in the map, this isn't an isogram
	if (LetterSeen.find(Letter) != LetterSeen.end())
	{
		//std::cout << "Has this letter been seen?: " << Letter << (LetterSeen.find(Letter) != LetterSeen.end()) << std::endl;
		return false;
	}
	else //Add the letter to the map to get checked against
	{
		LetterSeen.insert(std::make_pair(Letter, true));
	}
	

}

/* LEAVING IN FOR NOW
	This was a different way of arriving at the same answer
	It takes compares the size of the map array after adding
	all the letters to the size of the word. This worked fine
	since the map only adds an entry once and won't duplicate it
	However it's faster to check the above way because as soon as
	a duplicate is found it jumps out of adding the map.
*/
//If the container size doesn't equal the word size, it can't be an isogram
//if (LetterSeen.size() != Word.size())
//{
//	std::cout << "This is not an isogram..." << std::endl;
//	return false;
//}
////Else return true cause all letters were only seen once
//else
//{
//	std::cout << "Isogram confirmed!!!";
//	return true;
//}

return true;

}

Whoops, not sure why the formatting didn’t recognize the start of the coding.

Privacy & Terms