Initial commit of code for dopelib, and ch 1, 2, and 12
This commit is contained in:
21
libs/chapter1/is_unique.cpp
Normal file
21
libs/chapter1/is_unique.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <iostream>
|
||||
#include "chapter1.hpp"
|
||||
|
||||
/* Prompt
|
||||
Is Unique: Implement an algorithm to determine if a string
|
||||
has all unique characters. What if you cannot use additional data
|
||||
structures?
|
||||
*/
|
||||
|
||||
bool IsUnique(const char* s, int len){
|
||||
int tracker[255]= {};
|
||||
|
||||
for(int i=0; i<len; ++i){
|
||||
int tracker_index = static_cast<int>(s[i]);
|
||||
tracker[tracker_index] += 1;
|
||||
if (tracker[tracker_index] > 1){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user