Python - Capitalize repeated characters in a string
Last Updated :
20 Feb, 2023
- Given an input string with lowercase letters, the task is to write a python program to identify the repeated characters in the string and capitalize them.
Examples:
Input: programming language
Output: pRoGRAMMiNG lANGuAGe
Explanation: r,m,n,a,g are repeated elements
Input: geeks for geeks
Output: GEEKS for GEEKS
Explanation: g,e,k,s are repeated elements
Approach 1:
- We have to keep the character of a string as a key and the frequency of each character of the string as a value in the dictionary.
- Traverse the string and check the frequency of each character using a dictionary if the frequency of the character is greater than one then change the character to the uppercase using the upper() function.
Implementation:
Python3
# function for changing the
# repeated characters to uppercase
def RepeatedUpper(s):
# declaring dictionary
dic = {}
# Traversing the string
for i in s:
# If the character is already
# present in dictionary then increment
# the frequency of the character
if i in dic:
dic[i] = dic[i]+1
# If the character is not present in
# the dictionary then inserting
# the character in the dictionary
else:
dic[i] = 1
ans = ''
# traversing the string
for i in s:
# if the frequency of the character is
# greater than one
if dic[i] > 1:
# change into uppercase
i = i.upper()
# appending each character to the ans
ans = ans+i
return ans
# Driver code
s = 'geeks for geeks'
# function call
print(RepeatedUpper(s))
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach 2:
Using count() function.If count is greater than 1 then the character is repeated.Later on used upper() to convert to uppercase
Python3
s = "programming language"
new_str=""
for i in s:
if(i!="" and s.count(i)>1):
new_str+=i.upper()
else:
new_str+=i
print(new_str)
OutputpRoGRAMMiNG lANGuAGe
Time Complexity: O(n2) -> (count function + loop)
Auxiliary Space: O(n)
Approach 3: Using replace() and len() methods
Python3
#Capitalize repeated letters
s = "programming language"
length=len(s)
new_str=""
for i in s:
x=s.replace(i,"")
if(len(x)!=length-1):
new_str+=i.upper()
else:
new_str+=i
print(new_str)
OutputpRoGRAMMiNG lANGuAGe
Time Complexity: O(n2) -> (replace function + loop)
Auxiliary Space: O(n)
Approach #4 : Using Counter() function
Python3
from collections import Counter
s = "programming language"
new_str = ""
freq = Counter(s)
for i in s:
if(i != "" and freq[i] > 1):
new_str += i.upper()
else:
new_str += i
print(new_str)
OutputpRoGRAMMiNG lANGuAGe
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach #5 : Using List Comprehension
In this approach, we first create a Counter object to store the frequency of each character in the string. Then, we use a list comprehension to iterate over the characters in the string, and for each character, we check if its frequency is greater than 1. If it is, we convert it to uppercase using upper(). Finally, we use join() to concatenate the characters and return the result as a single string.
Python3
from collections import Counter
def capitalize_repeated_chars(s):
freq = Counter(s)
return "".join([char.upper() if freq[char] > 1 else char for char in s])
s = "programming language"
print(capitalize_repeated_chars(s))
OutputpRoGRAMMiNG lANGuAGe
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), for storing the frequency of each character in the Counter object.
Approach #6 : Using operator.countOf() method
- In this approach we iterate over the characters in the string.
- For each character, we check if its frequency(operator.countOf()) is greater than 1.
- Initialize a new_str with empty string.
- If it is, we convert it to uppercase using upper() and concatenate to new_str.
- If not concatenate the character as it is to new_str.
- Finally print the new_str.
Python3
s = "programming language"
new_str=""
import operator
for i in s:
if(i!="" and operator.countOf(s,i)>1):
new_str+=i.upper()
else:
new_str+=i
print(new_str)
OutputpRoGRAMMiNG lANGuAGe
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), for storing the frequency of each character.
Similar Reads
How to Capitalize First Character of String in Python Suppose we are given a string and we need to capitalize the first character of it, for example:Input: "geeks"Output: "Geeks"In this article, we are going to learn several ways of doing it with examples, let's look at them:Using str.capitalize()Using str.capitalize() is a simple way to make the first
2 min read
Python - Replace Different Characters in String at Once The task is to replace multiple different characters in a string simultaneously based on a given mapping. For example, given the string: s = "hello world" and replacements = {'h': 'H', 'o': '0', 'd': 'D'} after replacing the specified characters, the result will be: "Hell0 w0rlD"Using str.translate(
3 min read
Python - Insert characters at start and end of a string In Python programming, a String is an immutable datatype. But sometimes there may come a situation where we may need to manipulate a string, say add a character to a string. Letâs start with a simple example to insert characters at the start and end of a String in Python.Pythons = "For" s2 = "Geeks"
2 min read
Remove Special Characters from String in Python When working with text data in Python, it's common to encounter strings containing unwanted special characters such as punctuation, symbols or other non-alphanumeric elements. For example, given the input "Data!@Science#Rocks123", the desired output is "DataScienceRocks123". Let's explore different
2 min read
Python | K Character Split String The problems and at the same time applications of list splitting is quite common while working with python strings. Some characters are usually tend to ignore in the use cases. But sometimes, we might not need to omit those characters but include them in our programming output. Letâs discuss certain
4 min read