-
Notifications
You must be signed in to change notification settings - Fork 1
/
1-1.c
69 lines (55 loc) · 1.05 KB
/
1-1.c
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
//1.1 Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?
#include <stdio.h>
#include <stdbool.h>
//using bit operation to check
int is_uniq_using_big_operation(char *str)
{
int i = 0, flag = 0, value = 0;
for(i = 0; i < strlen(str); i++)
{
value = str[i] - 'a';
if(flag & (1 << value))
{
printf("String is not unique!! \n");
return false;
}
else
{
flag |= (1 << value);
}
}
printf("String is unique!! \n");
return true;
}
bool is_unique_string(char* p)
{
int ascii[256] = {0,};
if(!p)
{
printf("ERROR! \n");
return false;
}
while(*p != '\0')
{
if(ascii[*p] == 0)
{
ascii[*p] = 1;
p++;
}
else
{
printf("String is not unique!! \n");
return false;
}
}
printf("String is unique!! \n");
return true;
}
int main(void)
{
char* test = "abx";
bool rtn = false;
rtn = is_unique_string(test);
rtn = is_uniq_using_big_operation(test);
return 0;
}