Created
December 4, 2017 18:53
-
-
Save 4rchitect/2fe24fe4f51f6d2758783cb578030852 to your computer and use it in GitHub Desktop.
Solving lecture 45 challenge in O(n).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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