Header Ads Widget

Distinct Codes | codechef solution

 Sergey recently learned about country codes - two letter strings, denoting countries. For example, BY stands for Belarus and IN stands for India. Mesmerized by this new discovery, Sergey now looks for country codes everywhere!

Sergey has recently found a string S consisting of uppercase Latin letters. He wants to find the number of different country codes that appear in S as contiguous substrings. For the purpose of this problem, consider that every 2-letter uppercase string is a valid country code.

Input

The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.

The first and only line of each test case contains a string S, consisting of uppercase Latin letters.

Output

For each test case, output a single line containing the number of different country codes appearing in the given string.

Constraints

  • 1 ≤ T ≤ 100
  • Subtask 1 (35 points): 2 ≤ |S| ≤ 3
  • Subtask 2 (65 points): 2 ≤ |S| ≤ 104

Sample 1:

Input
Output
2
INBY
BYBY
3
2

Explanation:

Example case 1. The codes are INNB and BY.

Example case 2. The codes are BY and YB.

Code(C++):-

#include <bits/stdc++.h>
using namespace std;

int main() {
    // your code goes here
    int n;
    cin>>n;
    while(n--)
    {
     unordered_map<string ,int>mp;
     string s;
     cin>>s;
     string ans="";
     for(int i=0;s[i+1];i++)
     {
     ans="";
     ans+=s[i];
     ans+=s[i+1];
     mp[ans]+=1;
     }
     cout<<mp.size()<<endl;
    }
    return 0;
}


Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-


Post a Comment

0 Comments