TCS National Qualifier 2 Coding Question.
You are given a string and your task is to print the frequency of each character character.
Direction to Solve Problem
1. Take string from STDIN.
aaaabbBcddee
2. Get all different characters in give string using set().
set ={a, b, B, c, d, e} # unordered set
3. Iterate for different characters ( len(set )) because we only need to print a character one time and it count in input sting
range 0 to 5 i.e total 6 element
4. In every iteration take first character print it and its count.
now for 0 input_string[0] is 'a' and its count is 4
5 . Remove all occurrence of fist character, this will make next character as 1st character.
remove 'a' by replacing all 'a' in string by "" new input string will be bbBcddee
6. Repeat the same process, go to step 4.
7. Either print value to STDOUT on each iteration (python3) or print in one go(python2), your output will be same as
a4b2B1c1d2e2
Examples:
Input : aaaabbBcddee Output :a4b2B1c1d2e2 Input :aazzZ Output :a2z2Z1
# Python2 code here input_string = raw_input() temp_string ="" for _ in range(len(set(input_string))): temp_string += input_string[0] + str(input_string.count(input_string[0])) input_string = input_string.replace(input_string[0], "") print temp_string |
# Python3 code here input_string = input() for _ in range(len(set(input_string))): print(input_string[0]+str(input_string.count(input_string[0])), end ="") input_string = input_string.replace(input_string[0], "") |
Recommended Posts:
- TCS National Qualifier Test Kolkata 2018 Interview
- TCS Ninja National Qualifier Test 2020 Interview Experience ECE
- TCS Ninja Interview Experience from National Qualifier Test 2018 Delhi Region
- TCS NQT Coding Questions & How Coding Task Evaluated in TCS NQT
- National Instruments Interview Experience | Set 2
- National Instruments Interview Experience (Off-campus)
- National Instruments Interview Experience | Set 4 (On-Campus)
- National Instruments Interview Experience | Set 1 (Written Test)
- National Instruments Interview Experience | Set 3 (Programming Test)
- National Instruments Interview Experience | Set 5 (On-Campus - Internship)
- National Instruments (NI Tech) Software Engineer (For 1.5 years experienced)
- TCS Coding Practice Question | LCM of 2 Numbers
- TCS Coding Practice Question | HCF or GCD of 2 Numbers
- How to answer a coding question in an Interview?
- Informatica Coding Round Question
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



