Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9272669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:55:40+00:00 2026-06-18T15:55:40+00:00

I wrote the following program which should answer this question Write an efficient function

  • 0

I wrote the following program which should answer this question

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.

This is what I did:

#include <stdio.h>
#include <iostream>
#include <vector>
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->next = newN;
        return newN;
    }
}

void deleteNode(char ch, Node** head, Node**tail)
{
    Node *prev = NULL;
    Node *cur = *head;

    while(cur!=NULL)
    {
        if(cur->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)->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->next = cur->next;
                    delete cur;
                    return;
                }
            }
        }
        // no match keep searching
        prev = cur;
        cur = cur->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], &head, &tail);
            }break;
        }

    }

    if(head != NULL)
        printf("First char without repetition: %c", head->c);
    else
        printf("No char matched");

    return 0;
}

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).

What’s this program’s computational complexity with big-O notation?

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:55:41+00:00Added an answer on June 18, 2026 at 3:55 pm

    Well, yeah on the surface this looks like O(N) complexity, but you have introduced undesirable inefficiencies by using dynamic allocation.

    However, because you call deleteNode and that has to search through a list, you no longer have O(N) complexity.

    Think what happens if you have the string:

    abcdefghijklmnopqrstuvwxyzzyxwvutsrqponmlkjihgfedcb
    

    This has a complexity of roughly O(N*(N-1)/2) because every deleteNode call must scan to the end of the remaining list.

    All you really need to do is scan the string once and store the index of each character (if not found already), then scan that array for the lowest index. If you like, you can use an index of -1 to indicate a character has not been encountered. That would be complexity of O(2N)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following line in my C program should provided All/Group/Owner read and write permissions
I started to learn java yesterday and I wrote the followind program which should
Question. Write MIPS assembley code which is equivalent to the following Java code fragment.
I wrote the following program #include <iostream> template<typename C, typename Res, typename... Args> class
In Visual C++ i wrote the following sample in a C++ program: float f1
When I write the following program: file 1: #include <stdio.h> int global; void print_global1()
I cannot figure out why the following simple program doesn't (create) and then write
I have the following assignment: Write a complete 8086 program to perform the calculator
I need to write a simple program for work that does the following: read
So I have the following read and write file program for an Array. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.