• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Given a string, find the first character which is non-repetitive

April 13, 2014 by Dhaval Dave

Given a string, find the first character which is non -repetitive i.e that character must not be present anywhere else in the string.

With two different solutions.

Eg : Input : teeterson      
     Output : r, as it is the first element which 
                        is non repetitive.

Solution 1 :

1) Scan the string from left to right and construct the count array.
2) Again, scan the string from left to right and check for count of 
each character, if you find an element who's count is 1, return it.

Time Complexity : O(2N)= O(N)
Space Complexity : O(N)

Solution 2 :

To reduce Time for searching , we can use Hashmap.
Benefit from previous solution:
to get count for 2,3 etc we can easily use this function with little modification
To search for count 1, no need to iterate array again.

map < int , character > m;
map < int , character > : iterator it;
void *getNonRepChar(char *str)
{
    int *count = (int *)calloc(sizeof(int), NO_OF_CHARS);
    int i;
    for (i = 0; *(str+i);  i++){
       count[*(str+i)]++;
       m.insert(make_pair(count,*(str+i)));
    }//for ended
  return count;
}//function ended

 it.find(1);
 if(it != end() ){
   // element found
   character op = it->second ; 
   printd (" %c has count %d" ,op,1); 
  }
 else  
 printf ("Not found Such element");

Time Complexity : O(N)
Space Complexity : O(N)

-IF YOU LIKE OUR EFFORT, PLEASE LIKE AND SHARE OUR FB-PAGE AND SITE

Similar Articles

Filed Under: Hacker Earth Questions, Interview Questions, problem Tagged With: string

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Possible sizes of bus to carry n groups of friends

Find the kth number with prime factors 3, 5 and 7

Get K Max and Delete K Max in stream of incoming integers

Check if an array has duplicate numbers in O(n) time and O(1) space

Printing each word reverse in string

The greedy coins game Dynamic Programming

Find if two rectangles overlap

HackeEarth Flipkart’s Drone

25 horses 5 tracks Find 3 fastest puzzle

Length of the longest substring without repeating characters

Minimum insertions to form a palindrome

Coin Collection Dynamic Programming

flattens 2 D linked list to a single sorted link list

write a c program that given a set a of n numbers and another number x determines whether or not there exist two elements in s whose sum is exactly x

Fibonacci Hashing & Fastest Hashtable

Find loop in Singly linked list

Best Java Book | Top Java Programming Book for Beginners

Adobe Interview Questions 8 month Exp

Skiing on Mountains Matrix

Microsoft BING Interview Experience

Generate largest number arranging a no. of given non negative integer numbers

K’th Largest Element in BST when modification to BST is not allowed

Number of Islands BFS/DFS

Maximum path sum between two leaves

How Radix sort works

Apriori algorithm C Code Data Mining

Add Sub Multiply very large number stored as string

Find the number ABCD such that when multipled by 4 gives DCBA.

In Given LinkedList Divide LL in N Sub parts and delete first K nodes of each part

Find and print longest consecutive number sequence in a given sequence in O(n)

Copyright © 2026 · Genesis Framework · WordPress · Log in