Skip to content

Instantly share code, notes, and snippets.

@4rchitect
Created December 4, 2017 18:53
Show Gist options
  • Save 4rchitect/2fe24fe4f51f6d2758783cb578030852 to your computer and use it in GitHub Desktop.
Save 4rchitect/2fe24fe4f51f6d2758783cb578030852 to your computer and use it in GitHub Desktop.
Solving lecture 45 challenge in O(n).
bool IsWordIsogram(std::string Word)
{
// This create an array of size 26 initialized with 0 in every index.
int arr[26] = {};
// For each letter in the Word
for(int i = 0; i < Word.length(); i++)
{
// this subtract the value of character 'a' from the character that we are looking for like 'a'-'b' = 1;
int index = 'a'-Word[i];
// Go to that index and mark that we have a letter of that kind.
arr[index]++;
// Check if we have more than one letter of that kind.
if(arr[index] >= 2)
return false; // We have more than two letters of same kind, so it's not a isogram.
}
return true; // This word is a isogram.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment