-
Notifications
You must be signed in to change notification settings - Fork 7
/
154.cpp
57 lines (48 loc) · 1.17 KB
/
154.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
// C++ program to print all strings that contain all
// characters of a word
#include <bits/stdc++.h>
#include<stdio.h>
#include<string.h>
using namespace std;
# define NO_OF_CHARS 256
/* prints list items having all caharacters of word */
void print(char list[][50], char *word, int list_size)
{
/*Since calloc is used, map[] is initialized as 0 */
int *map = new int[(sizeof(int)*NO_OF_CHARS)];
int i, j, count, word_size;
/*Set the values in map */
for (i = 0; *(word+i); i++)
map[*(word + i)] = 1;
/* Get the length of given word */
word_size = strlen(word);
/* Check each item of list if has all characters
of word*/
for (i = 0; i < list_size; i++)
{
for (j = 0, count = 0; *(list[i] + j); j++)
{
if (map[*(list[i] + j)])
{
count++;
/* unset the bit so that strings like
sss not printed*/
map[*(list[i] + j)] = 0;
}
}
if (count == word_size)
cout << list[i] << endl;
/*Set the values in map for next item*/
for (j = 0; *(word + j); j++)
map[*(word + j)] = 1;
}
}
/* Driver code*/
int main()
{
char str[] = "sun";
char list[][50] = {"geeksforgeeks", "unsorted", "sunday",
"just", "sss" };
print(list, str, 5);
return 0;
}