forked from mandliya/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-1-hasUniqueChars.cpp
85 lines (69 loc) · 2.24 KB
/
1-1-hasUniqueChars.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* Cracking the coding interview, edition 6
* Problem 1.1
* Write an algorithm to determine whether a string has unique characters or not
* Can we do it without using additional data structures?
*/
#include <iostream>
#include <cstring>
/*
* Function hasUniqueChars1
* Args - std::string
* Output:: True if string has all characters which are unique
* False if string has at least one repeated char.
* Assumption:: ASCII chars, 256 chars.
*/
bool hasUniqueChars1( std::string str)
{
int len = str.length();
int check[8]; //represents 256 bits.
std::memset( check, 0 , sizeof check );
int idx, v, shift;
for ( int i = 0; i < len; ++i) {
v = (int) str[i];
idx = v / 32; //identify which int bucket will represent this char
shift = v % 32; //identify which bit in above int will represent the char
if ( check[idx] & ( 1 << shift ) ) {
return false;
}
check[idx] |= ( 1 << shift );
}
return true;
}
/*
* Function hasUniqueChars2
* Args - std::string
* Output:: True if string has all characters which are unique
* False if string has at least one repeated char.
* Assumption:: string only contains (a..z), 26 chars.
*/
bool hasUniqueChars2( std::string str)
{
int check = 0;
int len = str.length();
for (int i = 0; i < len; ++i)
{
int c = (int)(str[i] - 'a');
if ( check & ( 1 << c ) ) {
return false;
}
check |= ( 1 << c);
}
return true;
}
int main ()
{
std::string word1("copyrightable");
std::string word2("Dermatoglyphics!");
std::string word3("abiogenesis");
std::string word4("Centrobaric!");
//a word with unique chars (a-z)
std::cout << "Does " << word1 << " has unique chars :" << hasUniqueChars2(word1) << std::endl;
//a word with unique ASCII chars
std::cout << "Does " << word2 << " has unique chars :" << hasUniqueChars1(word2) << std::endl;
//a word with repeated chars (a-z)
std::cout << "Does " << word3 << " has unique chars :" << hasUniqueChars2(word3) << std::endl;
//a word with repeated AsCII chars
std::cout << "Does " << word4 << " has unique chars :" << hasUniqueChars1(word4) << std::endl;
return 0;
}