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
K'th Non-repeating Character Given a string str of length n (1 <= n <= 106) and a number k, the task is to find the kth non-repeating character in the string.Examples: Input : str = geeksforgeeks, k = 3Output : rExplanation: First non-repeating character is f, second is o and third is r.Input : str = geeksforgeeks, k = 2O
14 min read
Find the Earliest Repeating Character Given a string S of length n, the task is to find the earliest repeated character in it. The earliest repeated character means, the character that occurs more than once and whose second occurrence has the smallest index.Example:Input: s = "geeksforgeeks" Output: e Explanation: e is the first element
9 min read
Count K-Length Substrings With No Repeated Characters Given a string S and an integer k, the task is to return the number of substrings in S of length k with no repeated characters.Example:Input: S = "geeksforgeeks", k = 5Output: 4Explanation: There are 4 substrings, they are: 'eksfo', 'ksfor', 'sforg', 'forge'.Input: S = "home", k = 5Output: 0Explanat
6 min read
Python Dictionary to find mirror characters in a string Given a string and a number N, we need to mirror the characters from the N-th position up to the length of the string in alphabetical order. In mirror operation, we change âaâ to âzâ, âbâ to âyâ, and so on. Examples: Input : N = 3 paradox Output : paizwlc We mirror characters from position 3 to end.
2 min read
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