Can anybody tell me how to implement a program to check a string contains all unique chars ?
Use a 256-entry array. Fill it with 0. Now traverse the string setting the corresponding entry in the array to 1 if it's 0. Otherwise, there are repeated chars in the string.
I beleive there is a much simpler way:
int check_string_unique(char *str)
{
int i = 0;
int a = 0;
while (str[i])
{
a = i + 1; // peak to the next character
while (str[a])
{
if (str[i] == str[a]) // you found a match
return (0); // false
a++; // if you've checked a character before, there's no need to start at the beggining of the string each time. You only have to check with what is left.
}
i++; //check each character.
}
return (1); //true!
}
#include <stdio.h>
#define ARR_SIZE 32
unsigned char charFlag[ARR_SIZE];
void initFlag() {
int i = 0;
for (i = 0; i < ARR_SIZE; i++)
charFlag[i] = 0;
}
int getFlag(int position) {
int val = 0;
int flagMask = 1;
int byteIndex = position / 8;
int locPos = position % 8;
flagMask = flagMask << locPos;
// flagMask = ~flagMask;
val = charFlag[byteIndex] & flagMask;
val = !(!val);
// printf("\nhex: %x\n", val);
return val;
}
void setFlag(int position) {
int flagMask = 1;
int byteIndex = position / 8;
int locPos = position % 8;
flagMask = flagMask << locPos;
charFlag[byteIndex] = charFlag[byteIndex] | flagMask;
}
int isUniq(char *str) {
int is_uniq = 1;
do {
char *lStr = str;
int strLen = 0;
int i;
if (str == 0)
break;
while (*lStr != 0) {
lStr++;
strLen++;
}
initFlag();
lStr = str;
for (i = 0; i < strLen; i++) {
if (getFlag(lStr[i]))
break;
setFlag(lStr[i]);
}
if (i != strLen)
is_uniq = 0;
} while (0);
return is_uniq;
}
int main() {
char *p = "abcdefe";
printf("Uniq: %d\n", isUniq(p));
return 0;
}
Sort the characters in the string using your algorithm of choice (e.g. the builtin qsort
function), then scan the string checking for consecutive repeating letters; if you get to the end without finding any, the string contains all unique characters.
An alternative may be using some structure that has one bucket for each character the string may contain, all initialized to zero; you scan the string, incrementing the value of the bucket corresponding to the current character. If you get to increment a bucket that already has a 1 inside it you are sure that your string contains duplicates.
This can work fine with char
s and an array (of size UCHAR_MAX+1
), but it quickly gets out of hand when you start to deal with wide characters. In such case you would need a hashtable or some other "serious" container.
The best algorithm depends on the length of the strings to examine, the size of each character, the speed of the sorting algorithm and the cost of allocating/using the structure to hold the character frequencies.
bool isUnique(char st[],int size)
{
bool char_set[256]=false;
for(int i=0;i<size;i++)
{
if(char_set[st[i]]-'0')
return false;
char_set[st[i]-'0')=true;
}
return true;
}
Simple solution will be using 2 loops. No additional data structure is needed to keep a track on characters.
bool has_unique_char(char *str,int n)
{
if(n==0)
return true;
for(int i=1;i<n;i++){
for(int j=0;j<i;j++){
if(str[i] == str[j])
return false;
}
}
return true;
}