Given a string and a number k, find the k’th non-repeating character in the string. Consider a large input string with lacs of characters and a small character set. How to find the character by only doing only one traversal of input string?

**Examples:**

Input : str = geeksforgeeks, k = 3 Output : r First non-repeating character is f, second is o and third is r. Input : str = geeksforgeeks, k = 2 Output : o Input : str = geeksforgeeks, k = 4 Output : Less than k non-repeating characters in input.

This problem is mainly an extension of First non-repeating character problem.

**Method 1 (Simple : O(n ^{2})**

A Simple Solution is to run two loops. Start traversing from left side. For every character, check if it repeats or not. If the character doesn’t repeat, increment count of non-repeating characters. When the count becomes k, return the character.

**Method 2 (O(n) but requires two traversals)**

- Create an empty hash.
- Scan input string from left to right and insert values and their counts in the hash.
- Scan input string from left to right and keep count of characters with counts more than 1. When count becomes k, return the character.

**Method 3 (O(n) and requires one traversal)**

The idea is to use two auxiliary arrays of size 256 (Assuming that characters are stored using 8 bits). The two arrays are:

count[x] : Stores count of character 'x' in str. If x is not present, then it stores 0. index[x] : Stores indexes of non-repeating characters in str. If a character 'x' is not present or x is repeating, then it stores a value that cannot be a valid index in str[]. For example, length of string.

- Initialize all values in count[] as 0 and all values in index[] as n where n is length of string.
- Traverse the input string str and do following for every character c = str[i].
- Increment count[x].
- If count[x] is 1, then store index of x in index[x], i.e., index[x] = i
- If count[x] is 2, then remove x from index[], i.e., index[x] = n

- Now index[] has indexes of all non-repeating characters. Sort index[] in increasing order so that we get k’th smallest element at index[k]. Note that this step takes O(1) time because there are only 256 elements in index[].

Below is implementation of above idea.

## C++

`// C++ program to find k'th non-repeating character ` `// in a string ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `const` `int` `MAX_CHAR = 256; ` ` ` `// Returns index of k'th non-repeating character in ` `// given string str[] ` `int` `kthNonRepeating(string str, ` `int` `k) ` `{ ` ` ` `int` `n = str.length(); ` ` ` ` ` `// count[x] is going to store count of ` ` ` `// character 'x' in str. If x is not present, ` ` ` `// then it is going to store 0. ` ` ` `int` `count[MAX_CHAR]; ` ` ` ` ` `// index[x] is going to store index of character ` ` ` `// 'x' in str. If x is not present or x is ` ` ` `// repeating, then it is going to store a value ` ` ` `// (for example, length of string) that cannot be ` ` ` `// a valid index in str[] ` ` ` `int` `index[MAX_CHAR]; ` ` ` ` ` `// Initialize counts of all characters and indexes ` ` ` `// of non-repeating characters. ` ` ` `for` `(` `int` `i = 0; i < MAX_CHAR; i++) ` ` ` `{ ` ` ` `count[i] = 0; ` ` ` `index[i] = n; ` `// A value more than any index ` ` ` `// in str[] ` ` ` `} ` ` ` ` ` `// Traverse the input string ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `// Find current character and increment its ` ` ` `// count ` ` ` `char` `x = str[i]; ` ` ` `++count[x]; ` ` ` ` ` `// If this is first occurrence, then set value ` ` ` `// in index as index of it. ` ` ` `if` `(count[x] == 1) ` ` ` `index[x] = i; ` ` ` ` ` `// If character repeats, then remove it from ` ` ` `// index[] ` ` ` `if` `(count[x] == 2) ` ` ` `index[x] = n; ` ` ` `} ` ` ` ` ` `// Sort index[] in increasing order. This step ` ` ` `// takes O(1) time as size of index is 256 only ` ` ` `sort(index, index+MAX_CHAR); ` ` ` ` ` `// After sorting, if index[k-1] is value, then ` ` ` `// return it, else return -1. ` ` ` `return` `(index[k-1] != n)? index[k-1] : -1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str = ` `"geeksforgeeks"` `; ` ` ` `int` `k = 3; ` ` ` `int` `res = kthNonRepeating(str, k); ` ` ` `(res == -1)? cout << ` `"There are less than k non-"` ` ` `"repeating characters"` ` ` `: cout << ` `"k'th non-repeating character"` ` ` `" is "` `<< str[res]; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find k'th non-repeating character ` `// in a string ` ` ` `import` `java.util.Arrays; ` ` ` `class` `GFG ` `{ ` ` ` `public` `static` `int` `MAX_CHAR = ` `256` `; ` ` ` ` ` `// Returns index of k'th non-repeating character in ` ` ` `// given string str[] ` ` ` `static` `int` `kthNonRepeating(String str, ` `int` `k) ` ` ` `{ ` ` ` `int` `n = str.length(); ` ` ` ` ` `// count[x] is going to store count of ` ` ` `// character 'x' in str. If x is not present, ` ` ` `// then it is going to store 0. ` ` ` `int` `[] count = ` `new` `int` `[MAX_CHAR]; ` ` ` ` ` `// index[x] is going to store index of character ` ` ` `// 'x' in str. If x is not present or x is ` ` ` `// repeating, then it is going to store a value ` ` ` `// (for example, length of string) that cannot be ` ` ` `// a valid index in str[] ` ` ` `int` `[] index = ` `new` `int` `[MAX_CHAR]; ` ` ` ` ` `// Initialize counts of all characters and indexes ` ` ` `// of non-repeating characters. ` ` ` `for` `(` `int` `i = ` `0` `; i < MAX_CHAR; i++) ` ` ` `{ ` ` ` `count[i] = ` `0` `; ` ` ` `index[i] = n; ` `// A value more than any index ` ` ` `// in str[] ` ` ` `} ` ` ` ` ` `// Traverse the input string ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `// Find current character and increment its ` ` ` `// count ` ` ` `char` `x = str.charAt(i); ` ` ` `++count[x]; ` ` ` ` ` `// If this is first occurrence, then set value ` ` ` `// in index as index of it. ` ` ` `if` `(count[x] == ` `1` `) ` ` ` `index[x] = i; ` ` ` ` ` `// If character repeats, then remove it from ` ` ` `// index[] ` ` ` `if` `(count[x] == ` `2` `) ` ` ` `index[x] = n; ` ` ` `} ` ` ` ` ` `// Sort index[] in increasing order. This step ` ` ` `// takes O(1) time as size of index is 256 only ` ` ` `Arrays.sort(index); ` ` ` ` ` `// After sorting, if index[k-1] is value, then ` ` ` `// return it, else return -1. ` ` ` `return` `(index[k-` `1` `] != n)? index[k-` `1` `] : -` `1` `; ` ` ` `} ` ` ` ` ` `// driver program ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `String str = ` `"geeksforgeeks"` `; ` ` ` `int` `k = ` `3` `; ` ` ` `int` `res = kthNonRepeating(str, k); ` ` ` ` ` `System.out.println(res == -` `1` `? ` `"There are less than k non-repeating characters"` `: ` ` ` `"k'th non-repeating character is "` `+ str.charAt(res)); ` ` ` `} ` `} ` ` ` `// Contributed by Pramod Kumar ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to find k'th ` `# non-repeating character in a string ` `MAX_CHAR ` `=` `256` ` ` `# Returns index of k'th non-repeating ` `# character in given string str[] ` `def` `kthNonRepeating(` `str` `, k): ` ` ` ` ` `n ` `=` `len` `(` `str` `) ` ` ` ` ` `# count[x] is going to store count of ` ` ` `# character 'x' in str. If x is not ` ` ` `# present, then it is going to store 0. ` ` ` `count ` `=` `[` `0` `] ` `*` `MAX_CHAR ` ` ` ` ` `# index[x] is going to store index of ` ` ` `# character 'x' in str. If x is not ` ` ` `# present or x is repeating, then it ` ` ` `# is going to store a value (for example, ` ` ` `# length of string) that cannot be a valid ` ` ` `# index in str[] ` ` ` `index ` `=` `[` `0` `] ` `*` `MAX_CHAR ` ` ` ` ` `# Initialize counts of all characters ` ` ` `# and indexes of non-repeating characters. ` ` ` `for` `i ` `in` `range` `( MAX_CHAR): ` ` ` `count[i] ` `=` `0` ` ` `index[i] ` `=` `n ` `# A value more than any ` ` ` `# index in str[] ` ` ` ` ` `# Traverse the input string ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# Find current character and ` ` ` `# increment its count ` ` ` `x ` `=` `str` `[i] ` ` ` `count[` `ord` `(x)] ` `+` `=` `1` ` ` ` ` `# If this is first occurrence, then ` ` ` `# set value in index as index of it. ` ` ` `if` `(count[` `ord` `(x)] ` `=` `=` `1` `): ` ` ` `index[` `ord` `(x)] ` `=` `i ` ` ` ` ` `# If character repeats, then remove ` ` ` `# it from index[] ` ` ` `if` `(count[` `ord` `(x)] ` `=` `=` `2` `): ` ` ` `index[` `ord` `(x)] ` `=` `n ` ` ` ` ` `# Sort index[] in increasing order. This step ` ` ` `# takes O(1) time as size of index is 256 only ` ` ` `index.sort() ` ` ` ` ` `# After sorting, if index[k-1] is value, ` ` ` `# then return it, else return -1. ` ` ` `return` `index[k ` `-` `1` `] ` `if` `(index[k ` `-` `1` `] !` `=` `n) ` `else` `-` `1` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `str` `=` `"geeksforgeeks"` ` ` `k ` `=` `3` ` ` `res ` `=` `kthNonRepeating(` `str` `, k) ` ` ` `if` `(res ` `=` `=` `-` `1` `): ` ` ` `print` `(` `"There are less than k"` `, ` ` ` `"non-repeating characters"` `) ` ` ` `else` `: ` ` ` `print` `(` `"k'th non-repeating character is"` `, ` ` ` `str` `[res]) ` ` ` `# This code is contributed ` `# by ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find k'th non-repeating ` `// character in a string ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `public` `static` `int` `MAX_CHAR = 256; ` ` ` ` ` `// Returns index of k'th non-repeating ` ` ` `// character in given string str[] ` ` ` `static` `int` `kthNonRepeating(String str, ` `int` `k) ` ` ` `{ ` ` ` ` ` `int` `n = str.Length; ` ` ` ` ` `// count[x] is going to store count of ` ` ` `// character 'x' in str. If x is not ` ` ` `// present, then it is going to store 0. ` ` ` `int` `[]count = ` `new` `int` `[MAX_CHAR]; ` ` ` ` ` `// index[x] is going to store index of ` ` ` `// character 'x' in str. If x is not ` ` ` `// present or x is repeating, then it ` ` ` `// is going to store a value (for ` ` ` `// example, length of string) that ` ` ` `// cannot be a valid index in str[] ` ` ` `int` `[]index = ` `new` `int` `[MAX_CHAR]; ` ` ` ` ` `// Initialize counts of all characters ` ` ` `// and indexes of non-repeating ` ` ` `// characters. ` ` ` `for` `(` `int` `i = 0; i < MAX_CHAR; i++) ` ` ` `{ ` ` ` `count[i] = 0; ` ` ` ` ` `// A value more than any index ` ` ` `// in str[] ` ` ` `index[i] = n; ` ` ` `} ` ` ` ` ` `// Traverse the input string ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` ` ` `// Find current character and ` ` ` `// increment its count ` ` ` `char` `x = str[i]; ` ` ` `++count[x]; ` ` ` ` ` `// If this is first occurrence, ` ` ` `// then set value in index as ` ` ` `// index of it. ` ` ` `if` `(count[x] == 1) ` ` ` `index[x] = i; ` ` ` ` ` `// If character repeats, then ` ` ` `// remove it from index[] ` ` ` `if` `(count[x] == 2) ` ` ` `index[x] = n; ` ` ` `} ` ` ` ` ` `// Sort index[] in increasing order. ` ` ` `// This step takes O(1) time as size ` ` ` `// of index is 256 only ` ` ` `Array.Sort(index); ` ` ` ` ` `// After sorting, if index[k-1] is ` ` ` `// value, then return it, else ` ` ` `// return -1. ` ` ` `return` `(index[k-1] != n) ? ` ` ` `index[k-1] : -1; ` ` ` `} ` ` ` ` ` `// driver program ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `String str = ` `"geeksforgeeks"` `; ` ` ` `int` `k = 3; ` ` ` `int` `res = kthNonRepeating(str, k); ` ` ` ` ` `Console.Write(res == -1 ? ` `"There are less"` ` ` `+ ` `" than k non-repeating characters"` `: ` ` ` `"k'th non-repeating character is "` ` ` `+ str[res]); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal. ` |

*chevron_right*

*filter_none*

**Output:**

k'th non-repeating character is r

Space Optimized Solution :

This can be space optimized and can be solved using single index array only. Below is the space optimized solution:

`import` `java.util.*; ` ` ` `public` `class` `GFG { ` ` ` `public` `static` `int` `MAX_CHAR = ` `256` `; ` ` ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `final` `String input = ` `"geeksforgeeks"` `; ` ` ` `int` `k = ` `3` `; ` ` ` `int` `res = kthNonRepeating(input, k); ` ` ` ` ` `System.out.println(res == -` `1` `? ` `"There are less than k non-repeating characters"` `: ` ` ` `"k'th non-repeating character is "` `+ input.charAt(res)); ` ` ` `} ` ` ` ` ` `public` `static` `int` `kthNonRepeating(` `final` `String input, ` `final` `int` `k) { ` ` ` `final` `int` `inputLength = input.length(); ` ` ` ` ` `/* ` ` ` `* indexArr will store index of non-repeating characters, ` ` ` `* inputLength for characters not in input and ` ` ` `* inputLength+1 for repeated characters. ` ` ` `*/` ` ` `final` `int` `[] indexArr = ` `new` `int` `[MAX_CHAR]; ` ` ` ` ` `// initialize all values in indexArr as inputLength. ` ` ` `Arrays.fill(indexArr, inputLength); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < inputLength ; i++) { ` ` ` `final` `char` `c = input.charAt(i); ` ` ` `if` `(indexArr == inputLength) { ` ` ` `indexArr = i; ` ` ` `} ` `else` `{ ` ` ` `indexArr = inputLength + ` `2` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `Arrays.sort(indexArr); ` ` ` ` ` `return` `(indexArr[k-` `1` `] != inputLength) ? indexArr[k-` `1` `] : -` `1` `; ` ` ` `} ` `} ` `// Contributed by AK ` |

*chevron_right*

*filter_none*

**Output:**

k'th non-repeating character is r

This article is contributed by Shivam Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Longest Common Prefix using Character by Character Matching
- Find a string such that every character is lexicographically greater than its immediate next character
- Replace every character of string by character whose ASCII value is K times more than it
- Count substrings that starts with character X and ends with character Y
- Replace every character of a string by a different character
- Shortest distance to every other character from given character
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Map every character of one string to another such that all occurrences are mapped to the same character
- Modify the string such that every character gets replaced with the next character in the keyboard
- Replace all occurrences of character X with character Y in given string
- Nearest smaller character to a character K from a Sorted Array
- Return maximum occurring character in an input string
- Given a string, find its first non-repeating character
- Find the first non-repeating character from a stream of characters
- Find k'th character of decrypted string | Set 1
- Remove a character from a string to make it a palindrome
- Check for Palindrome after every character replacement Query
- Nth character in Concatenated Decimal String
- Count of character pairs at same distance as in English alphabets
- Maximum consecutive repeating character in string