• 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

Minimum insertions to form a palindrome

Trie Dictionary

Given Set of words or A String find whether chain is possible from these words or not

The Magic HackerEarth Nirvana solutions Hiring Challenge

Urban Ladder Written Test.

N teams are participating. each team plays twice with all other teams. Some of them will go to the semi final. Find Minimum and Maximum number of matches that a team has to win to qualify for finals ?

Reliance Jio Software Developer Interview Experience

Find the smallest window in a string containing all characters of another string

Search element in a matrix with all rows and columns in sorted order

Printing intermediate Integers between one element & next element of array

Get Minimum element in O(1) from input numbers or Stack

Reversal of LinkedList

strtok()

Memory Efficient LinkedList

Number of Islands BFS/DFS

Length of the longest substring without repeating characters

Find min element in Sorted Rotated Array (Without Duplicates)

Possible sizes of bus to carry n groups of friends

Convert Decimal to Roman numbers / Romanizer HackerEarth Code

Maximum path sum between two leaves

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

1014 Practice Question of New GRE – Princeton

LeetCode: Binary Tree Maximum Path Sum

Puzzle : 100 doors in a row Visit and Toggle the door. What state the door will be after nth pass ?

CodeChef’ RRCOPY

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Cisco Hiring Event 21st – 22nd Feb 2015

C Program for TAIL command of UNIX

Find if a binary tree is height balanced ?

Check Binary Tree is Binary Search Tree or not

Copyright © 2026 · Genesis Framework · WordPress · Log in