Greedy, always choose the remaining one that is the lexicographically
smallest since choose any other way will yield a lexicographically
greater one.
void concante(char **strings, int n){
qsort(strings,n,sizeof(char *),compareStr);
}
int compareStr(const void *a, const void *b){
return strcmp(*(char **)a,*(char **)b);
}
On Feb 16, 10:05 pm, simplyamey <[email protected]> wrote:
> Try this code.
>
> #include<iostream>
> #include<string>
> using namespace std;
> int main()
> {
> string sArray[5] ={"aab","abc","bba","acc","bcc"};
>
> int n = 5;
>
> for(int i = 0;i < n; i++)
> {
> for( int j = 0; j < n - 1 ; j++)
> {
> if(sArray[j] > sArray[j+1])
> {
> string temp = sArray[j];
> sArray[j] = sArray[j + 1];
> sArray[j+1]= temp;
> }
> }
> }
>
> string sTemp;
> for(int k = 0; k < n ;k++)
> {
> sTemp = sTemp + sArray[k];}
>
> cout<<"printing result::"<< sTemp<<endl;
>
> }
>
> n = Number of strings.
>
> On Feb 17, 9:56 am, bittu <[email protected]> wrote:
>
>
>
>
>
>
>
> > Given a group of strings, find a string by concatenating all the
> > strings in any order which produces the lexicographically smallest
> > string.
> > For e.g strings are acc bcc abb
> > So the string formed should be abbaccbcc
>
> > Thanks
> > Shashank
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.