Responsive Ads Here

Thursday, 11 May 2017

Repetition of k length substring (write only function)

problem id:-http://practice.geeksforgeeks.org/problems/repetition-of-k-length-substring/1

code:--
bool checkString(string str, int k)
{
int l=str.length();
if(l%k==0)
    {
    if(l<=2*k)
        return true;
    unordered_map<string,int> m;
    int i=0;
    while(str[i]!='\0')
        {
        int j=0;
        string st="";
        while(j<k)
            {
            st+=str[i];
            ++j;
            ++i;
            }
        ++m[st];
        }
    if(m.size()==1)
        return true;
    else if(m.size()>2)
        return false;
    for(unordered_map<string,int> ::iterator itr=m.begin();itr!=m.end();++itr)
        {
        if((*itr).second==l/k-1)
            return true;
        }
    return false;
    }
return false;
}

No comments:

Post a Comment