c - What is the time complexity of my codes: reversing words in a string? Should I account for asymmetrical spacing? -
problem statement: write function of declaration void str_words_in_rev(char *input, int length)
reverses words in string of word/(s). i've implemented 2 different solutions it, 1 of extremely complicated.
solution 1: reverse entire string using void reverse(char* string, int start, int end)
function takes index of first , last letter of string or substring want reverse. after that, reverse each word in string using same reverse function.
#include<stdio.h> void swapchar(char *a, char *b) { char temp = *a; *a = *b; *b = temp; } int length(char *string) { if(string == null) return -1; int i=0; for(;string[i++];); return i; } void reverse(char* string, int start, int end) { int mid = start + (end - start + 1)/2; for(int i=start,j=end;i<mid;) swapchar(&string[i++], &string[j--]); } int main() { char str[] = "abc def ghij klmn"; int i=0, j=0, len = length(str), flag = 0; if(str == null || !(*str)) return 0; for(;str[j]!='\0';++j) { if(str[j] == ' ') { flag = 1; reverse(str,i,j-1); = j+1; } } if(flag == 1) { reverse(str, i, j-1); reverse(str,0,len-2); } printf("%s\n", str); return 0; }
solution 2: shift every word , following space character (after rearranging it) towards end using pushtoend function , then shift end before end word pushed. there printf statements can commented out understand what's happening, if explanation inadequate (which apologise for).
int pushtoend(char *str, int length) { int len = 0; int = 0, j = 0, k = 0, n = 0; (i = 0; str[i++] != ' ' && <= length;) ++len; if (len == length) return len; //printf("length of first word:"); //printf("%d\n", len); (j = - 1; j>0; --j) swapchar(&str[j], &str[j - 1]); ++len; //printf("after rearranging first word:"); //printf("%s\n", str); //printf("pushing first word toward end\n"); (; k + 2 * len <= length; k = k + len) { (int l = k; l<k + len; ++l) { swapchar(&str[l], &str[l + len]); } //printf("---%s---\n", str); } //printf("as blocks:"); //printf("%s\n", str); //printf("k value: %d\n", k); (; k + len<length; ++k) { (n = k + len; n>k; --n) { //printf("n value: %d\n", n); swapchar(&str[n], &str[n - 1]); } } //printf("one one:"); //printf("%s\n", str); return len; } void str_words_in_rev1(char *input, int length){ int len = 0; (int end = length; end>0; end = end - len) { //printf("end:%d\n", end); len = pushtoend(input, end); } }
once you've understood problem statement , solutions i've implemented, here questions:
what time complexity of solutions? think first solution has o(n) complexity , second 1 o(n^2) i'm not sure.
neither solutions account asymmetrical spacing. words reversed , spacing reversed well. it's imperceptible in cases there symmetrical spacing/multi-symmetrical spacing. problem statement unclear on this, i'd views on whether should accounted for.
solution1 o(n) time complexity. as,
solution2 o(kn) k number of words in string, in worst case words single characters o(n^2). , moving word word towards end , swapping other words towards front, o(kn).
Comments
Post a Comment