Getting WA

Problem Statement:
Logesh is a unique guy! He always finds geeky stuff in his way around. He is happy today being gifted with a baby boy. Now its time for him to think of a name. But he don’t want to spend time in banging his head. He took a lucky string and considering two lucky indices X and Y. The characters at X and Y are interchangeable and the remaining can be shuffled with their positions ending up in different names. Help him to find the number of different possible names given the lucky string and the lucky positions X and Y.

(Assume index starts with 0)

INPUT

The first input T, should be the number of test cases. Strings S, position X , position Y are given in the next T lines.
(String contains either all uppercase or all lowercase alphabets)

OUTPUT

For each test case, output the total number of different possible names

CONSTRAINTS

1 <= T <= 5
2<=String length <= 100

SAMPLE INPUT

2

alice 2 4

anand 1 3

SAMPLE OUTPUT

12

3

here is my code in C

fact(int n){
    if(n==0)
        return 1;
	if(n>1)
		n=n*fact(n-1);
	return n;
}

main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        char str[100],ch;
        int pos,pos1,i,hash[26]={0},count=0;
        scanf("%s %d %d",str,&pos,&pos1);
        if(str[0]>='a' && str[0]<='z')
            ch='a';
        else
            ch='A';
        for(i=0;str[i];i++)
        {
            if(!hash[str[i]-ch] && pos!=i && pos1!=i)
                count++;
            hash[str[i]-ch]++;
        }
        if(str[pos]!=str[pos1])
            printf("%d\n",2*fact(count));
        else if(pos==pos1)
            printf("%d\n",fact(count));
        else
            printf("%d\n",fact(count)+1);
    }
    return 0;
}
1 Like

Consider the input below :
2
alice 2 2
aa 1 2
Your code gives answer :
25
2
The correct answer is :
24
1
Your logic is wrong obviously . If I tell the logic then that will be like me solving the problem for you . However if you are stuck , let me know and I will post the logic also . Also , can you please tell me the problem code of this problem . I was not able to find this is in practice section .

1 Like

for second case 2 will cross the string length as index starts with 0

1 Like

Okay , yeah , i didn’t notice the 0-indexing thing . The examples are still valid though and your code incorrect for the same reasons .

2 Likes

Yeah , so one position gets fixed since x and y are same and rest can be permuted in 4! ways which is equal to 24 . Your logic of 2 * fact(count) and fact(count) + 1 is wrong . The number of permutations of n things in which a1 are of first kind , a2 of second kind , and so on is given by : n!/(a1!a2!..)

2 Likes

now also my logic wrong??

1 Like