Note that there are some explanatory texts on larger screens.

plurals
  1. POComputational complexity of a program
    primarykey
    data
    text
    <p>I wrote the following program which should answer this question</p> <blockquote> <p>Write an efficient function to find the first nonrepeated character in a string. For instance, the first nonrepeated character in “total” is 'o' and the first nonrepeated character in “teeter” is 'r'. Discuss the efficiency of your algorithm.</p> </blockquote> <p>This is what I did:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;iostream&gt; #include &lt;vector&gt; using namespace std; class Node { public: Node::Node(char ch) { c = ch; next = NULL; } char c; Node *next; }; Node* addNode(Node *tail, char ch) { if(tail == NULL) return new Node(ch); else { Node *newN = new Node(ch); tail-&gt;next = newN; return newN; } } void deleteNode(char ch, Node** head, Node**tail) { Node *prev = NULL; Node *cur = *head; while(cur!=NULL) { if(cur-&gt;c == ch) { // found cut it if(prev == NULL) { // head cut off if(*tail == *head) { // worst possible, just one element delete *head; *head = NULL; return; } else { // Head cut off but not just first element Node *tmp = *head; *head = (*head)-&gt;next; delete tmp; return; } } else { // delete normal node if(*tail == cur) { // delete tail Node *tmp = *tail; *tail = prev; delete tmp; return; } else { // Normal node not tail prev-&gt;next = cur-&gt;next; delete cur; return; } } } // no match keep searching prev = cur; cur = cur-&gt;next; } } int main() { char str[] = "total"; char htable[26]; memset(htable, 0, sizeof(char)*26); Node *head = NULL; Node *tail = head; for(unsigned int i=0;;i++) { if(str[i] == '\0') break; // check first match char m = htable[str[i]-'a']; switch(m) { case 0: { // first time, add it to linked list htable[str[i]-'a']++; tail = addNode(tail, str[i]); if(head == NULL) head = tail; }break; case 1: { // bam, cut it out htable[str[i]-'a']++; deleteNode(str[i], &amp;head, &amp;tail); }break; } } if(head != NULL) printf("First char without repetition: %c", head-&gt;c); else printf("No char matched"); return 0; } </code></pre> <p>and it works (although I didn't free the memory at the end of the program for the linked list). Basically I keep an hashtable with a 0 if a character hasn't been found yet, a 1 if it has been found once (and it's added to the linked list at the tail position) and 2 if there are at least two occurrences of it (and should be removed by the linked list).</p> <p><strong>What's this program's computational complexity with big-O notation?</strong></p> <p>Since this algorithm just passes once per each element I think it's O(n), although the removal of the values in the linked list (in the worst case possible) would require additional O(k^2) where k is the length of the alphabet used. Something like O(n+k^2) it's my pick and if the string is very long and the alphabet restricted the algorithm becomes very efficient.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload