Home | Projects | Notes > Problem Solving > LC - E - 796. Rotate String (sprintf, strstr)
This solution uses C standard library string manipulation functions such as sprintf
, strstr
and strlen
.
Complexity Analysis:
s
Solution:
xxxxxxxxxx
181// sprintf
2// strstr, strlen
3
4bool rotateString(char * s, char * goal){
5 if (strlen(s) != strlen(goal))
6 return false;
7
8 // dynamically allocate memory for 'str' and initialize all bytes with 0 ('\0')
9 char *str = (char *)calloc(strlen(s) * 2 + 1, sizeof(char));
10 sprintf(str, "%s%s", s, s);
11
12 // thanks to 'calloc' the very last elelement of 'str' is already '\0'
13
14 if (strstr(str, goal) == NULL)
15 return false;
16
17 return true;
18}