Saturday 3 March 2018

Space Less Hacker Rank Solution in C++

https://www.hackerrank.com/contests/ccollesium/challenges/space-less

solution:
#include <bits/stdc++.h>
using namespace std;
int main(void)
{

   int t;
   cin>>t;
   while(t--)
   {
        int num,c=0;
        string k;
        cin>>num;
     cin>>k;
        char st[1000000]={0};
        int n = 0;
        for (int i = 1; i <= num; i++)
            n += sprintf (&st[n], "%d", i); // sprintf returns the number of characters written
        string s(st);                       // converting a character array to string
        //the data typt for f must be size_t since we used find
        for(size_t f = 0 ;(f = s.find( k, f )) < n; f++) // we can use string::npos instead of n to loop until the end of the string".
            c++;
        cout<<c<<endl;  
   }
    return 0;
}

No comments:

Post a Comment