Monday, April 2, 2012

Program in C++

Problem Statement :


Given a string, find the substring with minimum length from it which contains exactly equal no of characters given in a hashmap which contains key as character and values as count of character needed in substring.


Solution:


//substring.cpp


#include "iostream.h"
#include "cstdio"
#include "string.h"
#include "map.h"


using namespace std;


string find_substr(map<char,int> mapcpy,string s)
{


map
<char,int>
mapcopy;
char x;
unsigned int tot_len=0,l=0;
int i=0;
int pos=0;


//calculating the minimum length of the substring , which is nothing but the sum of value field in the map for each key.


for(x='a'; x<'z'; x++)
{
tot_len += mapcpy[x];
}
//if the length of string entered is less than the minimum length required, there is no use of checking.
if(s.length() < tot_len)
return " ";


//checking index position in outer loop while traversing the string.


while( i < s.length() && s.length()-i >=tot_len )
{
l=tot_len;
mapcopy=mapcpy;
while(s[i]!='\0')
{
//if the value of key s[i] is 0, this means either s[i] is not required to be present //or the required no of its occurence has already occured.


if(mapcopy[s[i]] == 0)
break;
mapcopy[s[i]]--; //decreasing count of character found in string
i++; //increasing index
l--; //decresing min length with each char found
if(l==0) //purpose served if l==0
{
return(s.substr(pos,tot_len));
}


}
i=++pos; //incrementing index to next position in the string from where it started //earlier.
}
return " ";


}


int main()
{
map
<char,int>
map1;
string s_name;
string output;


//inserting values in the map for certain characters.


map1.insert(pair('a',2));
map1.insert(pair('m',1));
map1.insert(pair('d',1));
map1.insert(pair('g',1));
map1.insert(pair('r',1));
map1.insert(pair('i',1));


//Entering a string in which the substring needs to be found.


cout<<"Enter a string :";
getline(cin,s_name,'\n');


//output gets the first occurence of substring stored.
output = find_substr(map1,s_name);
cout<<"The String is : "<output;
}


Compiling : g++ -o output substring.cpp


Running output : ./output

10 comments:

  1. Got this link from careercup, the code seems easy to implement but this particular snippet is tough to understand, perhaps commenting code or giving a pseudo algorithm would help.

    Thanks!

    ReplyDelete
  2. where are you checking smallest substring ? you are just returning first sub string that contains all the character in the map ? there can be many substring, and the smallest need to be returned.

    ReplyDelete
    Replies
    1. there is nothing like smallest string as the question says it has to have that many characters in total as there are in the map specified by value for each character. So, the required length is tot_len.I am returning the first occurence of any of such string.We can return all by storing it in some other structure and returning it as a whole.

      Delete
  3. Can you see "substring with minimum length " ?what does this mean?

    ReplyDelete
    Replies
    1. I can..but read this :
      that contains "exactly equal no of characters given in a hashmap" which contains key as character and "values as count of character needed in substring".
      That is just to make the question complex.
      when all the characters are needed and their count value is also to be fulfilled ...how can the length differ?

      Delete
    2. If a character is not in the hashmap, it can occur as many time as possible.
      That's where "substring with minimum length" plays.

      Delete
    3. If we include extra characters, then how could the substring contains
      "Exactly equal number of characters as there are in the hashmap".

      By the example given :

      String : abcrefbda
      output : bda

      as given.

      In that case ab can be a solution or you want to say that all those characters in hashmap must be included and extra characters can be inserted if substring is not formed in contiguous, without extra chars..
      is tht so??

      Delete
    4. In that case, if string is changed to

      String : abcrefbcda

      Output should be changed to

      Output : bcda

      In my program, it will give string not found.

      Delete