-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsherlock_valid_string.c
More file actions
169 lines (133 loc) · 4.92 KB
/
Copy pathsherlock_valid_string.c
File metadata and controls
169 lines (133 loc) · 4.92 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
https://www.hackerrank.com/challenges/sherlock-and-valid-string/problem
I am printing 'YES' here, but the site says the Expected Output is 'YES',
but it still says I am returning the wrong answer.
*/
char* readline();
// Complete the isValid function below.
// Please either make the string static or allocate on the heap. For example,
// static char str[] = "hello world";
// return str;
//
// OR
//
// char* str = "hello world";
// return str;
//
/*
Valid strings:
1. Each character occurs the same number of times.
2. If one character can be removed to satisfy 1. the string is valid.
*/
char* isValid(char* s) {
static char * ret[2] = { "YES" , "NO" };
int count[256] = {0};
int slen = strlen(s);
int max = 0, min = slen+1, mncnt=0, mxcnt=0,k;
for ( k=0; k<slen; ++k ) {
/* Count the numer of times a character occurs. */
count[s[k]]++;
}
for ( k=0; k<256; ++k ) {
if ( 0==count[k] ) {
continue;
}
max = ( max < count[k] ) ? count[k] : max;
min = ( min > count[k] ) ? count[k] : min;
}
if ( max==min ) {
/* All characters occur the same number of times */
return ret[0];
}
/*
Handle special cases.
First count the number of characters that are maxes and mins.
*/
for ( k=0; k<256; ++k ) {
if ( count[k] ) {
printf("'%c' -> %d\n",k,count[k]);
if ( max==count[k] ) {
mxcnt++;
} else {
mncnt++;
}
}
}
printf("max = %d, mxcnt = %d, min = %d, mncnt = %d\n",
max,mxcnt,min,mncnt);
if ( 1==min && 1==mncnt ) {
/* If one character occurs once, it can simply be removed */
return ret[0];
} else if ( 1==mxcnt && (1==(max-min)) ) {
/*
If there is only one max occuring character, one of them can
be removed to equalize the occurrence with the mins.
*/
return ret[0];
}
printf("[%d] Here\n",__LINE__);
return ret[1];
}
/* ------------------------------------------------------------------------ */
char * test_fail = "ibfdgaeadiaefgbhbdghhhbgdfgeiccbiehhfcggchgghadhdhagfbahhddgghbdehidbibaeaagaeeigffcebfbaieggabcfbiiedcabfihchdfabifahcbhagccbdfifhghcadfiadeeaheeddddiecaicbgigccageicehfdhdgafaddhffadigfhhcaedcedecafeacbdacgfgfeeibgaiffdehigebhhehiaahfidibccdcdagifgaihacihadecgifihbebffebdfbchbgigeccahgihbcbcaggebaaafgfedbfgagfediddghdgbgehhhifhgcedechahidcbchebheihaadbbbiaiccededchdagfhccfdefigfibifabeiaccghcegfbcghaefifbachebaacbhbfgfddeceababbacgffbagidebeadfihaefefegbghgddbbgddeehgfbhafbccidebgehifafgbghafacgfdccgifdcbbbidfifhdaibgigebigaedeaaiadegfefbhacgddhchgcbgcaeaieiegiffchbgbebgbehbbfcebciiagacaiechdigbgbghefcahgbhfibhedaeeiffebdiabcifgccdefabccdghehfibfiifdaicfedagahhdcbhbicdgibgcedieihcichadgchgbdcdagaihebbabhibcihicadgadfcihdheefbhffiageddhgahaidfdhhdbgciiaciegchiiebfbcbhaeagccfhbfhaddagnfieihghfbaggiffbbfbecgaiiidccdceadbbdfgigibgcgchafccdchgifdeieicbaididhfcfdedbhaadedfageigfdehgcdaecaebebebfcieaecfagfdieaefdiedbcadchabhebgehiidfcgahcdhcdhgchhiiheffiifeegcfdgbdeffhgeghdfhbfbifgidcafbfcd";
//YES
int main ( ) {
char * answer = isValid(test_fail);
printf("[%d] test_fail = %s\n",__LINE__,answer);
return 0;
}
#define NSTRINGS 4
int main_old ( int argc, char ** argv ) {
char * test_strings[NSTRINGS] = { "aabbcd", "aabbccddeefghi",
"abcdefghhgfedecba", test_fail };
char * test_expect[NSTRINGS] = { "NO", "NO", "YES", "YES" };
int k;
for ( k=0; k<NSTRINGS; ++k ) {
printf("[%d] string - '%s'\n",__LINE__,test_strings[k]);
printf(" exp: %s\n",test_expect[k]);
printf(" %s\n",isValid(test_strings[k]));
}
return 0;
}
/* ------------------------------------------------------------------------ */
#if 0
int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
char* s = readline();
char* result = isValid(s);
fprintf(fptr, "%s\n", result);
fclose(fptr);
return 0;
}
char* readline() {
size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);
while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);
if (!line) { break; }
data_length += strlen(cursor);
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }
size_t new_length = alloc_length << 1;
data = realloc(data, new_length);
if (!data) { break; }
alloc_length = new_length;
}
if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';
}
data = realloc(data, data_length);
return data;
}
#endif