K’th Non-repeating Character in Python
Last Updated :
23 Jan, 2025
We need to find the first K characters in a string that do not repeat within the string. This involves identifying unique characters and their order of appearance. We are given a string s = "geeksforgeeks" we need to return the non repeating character from the string which is 'r' in this case. This can be done using multiple functions list comprehension and many other function.
Using OrderedDict and List Comprehension
Solution uses OrderedDict
to count character frequencies while maintaining their order of appearance. It then uses list comprehension to filter non-repeating characters and retrieves the K'th one if available.
Python
from collections import OrderedDict
s = "geeksforgeeks"
k = 3
# Count frequency of each character using OrderedDict
freq = OrderedDict()
for char in s:
freq[char] = freq.get(char, 0) + 1
# Use list comprehension to filter non-repeating characters
a = [char for char, count in freq.items() if count == 1]
# Return the K'th non-repeating character
if k <= len(a):
print(a[k - 1])
else:
print(None)
Explanatinon:
- Code uses an
OrderedDict
to count the frequency of each character in the string s
while maintaining the order of their first occurrence. - It filters the non-repeating characters using list comprehension and then checks if there are enough non-repeating characters to return the K'th one; if so, it prints it, otherwise prints
None.
Using a Regular Dictionary and List Comprehension
Solution uses a regular dictionary to count character frequencies while iterating through the string. List comprehension then filters out non-repeating characters, allowing retrieval of the K'th non-repeating character if available.
Python
s = "geeksforgeeks"
k = 3
# Count frequency of each character using a regular dictionary
freq = {}
for char in s:
freq[char] = freq.get(char, 0) + 1
# Use list comprehension to filter non-repeating characters
a = [char for char in s if freq[char] == 1]
# Return the K'th non-repeating character
if k <= len(a):
print(a[k - 1])
else:
print(None)
Explanation:
- Code counts the frequency of each character in the string
s
using a regular dictionary and then filters out the non-repeating characters by checking the frequency of each character. - It then checks if the list of non-repeating characters has enough elements to return the K'th non-repeating character, printing it if available, or
None
otherwise.
Using a Single Loop
Solution counts character frequencies and extracts non-repeating characters in a single loop, maintaining the order of their appearance. It then checks if there are enough non-repeating characters to return the K'th one.
Python
s = "geeksforgeeks"
k = 3
# Create a dictionary to store the frequency of each character
freq = {}
# First pass: Count frequency of each character
for char in s:
freq[char] = freq.get(char, 0) + 1
# Second pass: Find non-repeating characters
a = []
for char in s:
if freq[char] == 1:
a.append(char)
# Return the K'th non-repeating character
if k <= len(a):
print(a[k - 1]) # Output: 'r'
else:
print(None)
Explanation:
- Code performs two passes: in the first pass, it counts the frequency of each character in the string
s
using a dictionary in the second pass, it iterates through the string again to collect non-repeating characters. - It checks if there are enough non-repeating characters to return the K'th one, printing it if available, or
None
if the list is too short
Using collections.Counter
Solution uses collections.Counter
to count the frequency of each character in the string. It then filters out non-repeating characters using list comprehension and retrieves the K'th non-repeating character if available
Python
from collections import Counter
s = "geeksforgeeks"
k = 3
# Count frequency of characters using Counter
freq = Counter(s)
# Extract non-repeating characters
a = [char for char in s if freq[char] == 1]
# Return the K'th non-repeating character
if k <= len(a):
print(a[k - 1]) # Output: 'r'
else:
print(None)
Explanation:
- Code uses
Counter
from the collections
module to count the frequency of each character in the string s
. - It then extracts the non-repeating characters using a list comprehension and checks if there are enough non-repeating characters to return the K'th one, printing it if available or
None
otherwise.
Similar Reads
Count the number of Unique Characters in a String in Python We are given a string, and our task is to find the number of unique characters in it. For example, if the string is "hello world", the unique characters are {h, e, l, o, w, r, d}, so the output should be 8.Using setSet in Python is an unordered collection of unique elements automatically removing du
2 min read
Find all duplicate characters in string in Python In this article, we will explore various methods to find all duplicate characters in string. The simplest approach is by using a loop with dictionary.Using Loop with DictionaryWe can use a for loop to find duplicate characters efficiently. First we count the occurrences of each character by iteratin
2 min read
Find Frequency of Characters in Python In this article, we will explore various methods to count the frequency of characters in a given string. One simple method to count frequency of each character is by using a dictionary.Using DictionaryThe idea is to traverse through each character in the string and keep a count of how many times it
2 min read
Find Frequency of Characters in Python In this article, we will explore various methods to count the frequency of characters in a given string. One simple method to count frequency of each character is by using a dictionary.Using DictionaryThe idea is to traverse through each character in the string and keep a count of how many times it
2 min read
How to Take Only a Single Character as an Input in Python Through this article, you will learn how to accept only one character as input from the user in Python. Prompting the user again and again for a single character To accept only a single character from the user input: Run a while loop to iterate until and unless the user inputs a single character.If
3 min read
How to Take Only a Single Character as an Input in Python Through this article, you will learn how to accept only one character as input from the user in Python. Prompting the user again and again for a single character To accept only a single character from the user input: Run a while loop to iterate until and unless the user inputs a single character.If
3 min read