Saturday, December 28, 2024
Google search engine
HomeLanguagesTCS National Qualifier 2 Coding Question.

TCS National Qualifier 2 Coding Question.

You are given a string and your task is to print the frequency of each character.

Direction to Solve Problem:

1. Take string from STDIN.  

aaaabbBcddee

2. Get all different characters in given 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 first 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

C++




// C++ program for the above approach
 
#include <iostream>
#include <string>
#include <set>
 
using namespace std;
 
int main() {
    string input_string;
    getline(cin, input_string);
    string temp_string = "";
    set<char> char_set(input_string.begin(), input_string.end());
    for (auto c : char_set) {
        temp_string += c + to_string(count(input_string.begin(), input_string.end(), c));
    }
    cout << temp_string << endl;
    return 0;
}
 
 
// This code is contributed by codebraxnzt


Java




import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String inputString = scanner.nextLine();
        String tempString = "";
        Set<Character> charSet = new HashSet<>();
         
        for (char c : inputString.toCharArray()) {
            charSet.add(c);
        }
 
        for (char c : charSet) {
            tempString += c + Integer.toString(countCharOccurrences(inputString, c));
        }
 
        System.out.println(tempString);
    }
 
    static int countCharOccurrences(String str, char target) {
        int count = 0;
        for (char c : str.toCharArray()) {
            if (c == target) {
                count++;
            }
        }
        return count;
    }
}


Python




# 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"


C#




//Here's the equivalent C# code:
 
using System;
using System.Collections.Generic;
using System.Linq;
 
class Gfg {
    static void Main() {
        string inputString = Console.ReadLine();
        string tempString = "";
        var charSet = new HashSet<char>(inputString);
        foreach (var c in charSet) {
            tempString += c + (inputString.Count(x => x == c)).ToString();
        }
        Console.WriteLine(tempString);
    }
}


Javascript




// JavaScript code here
let input_string = prompt();
let temp_string = "";
 
for (let c of new Set(input_string)) {
temp_string += c + input_string.split(c).length - 1;
input_string = input_string.replaceAll(c, "");
}
 
console.log(temp_string);


Python3




# 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], "")


RELATED ARTICLES

Most Popular

Recent Comments