Home > Software design >  Please help me find what case am i missing
Please help me find what case am i missing

Time:05-28

This is the question: In a far away Galaxy of Tilky Way, there was a planet Tarth where the sport of Tompetitive Toding was very popular. According to legends, there lived a setter known to give advanced string manipulation problems disguised as cakewalk problems.

thef, the king of thefland loved the letter 't'. And now, he has made a game on it with his ministers! His ministers would give him 2 strings, a and b. thef has to make them exactly same, for which he can do the following-

Step 1 - He MAY reverse at most one of the strings. (This step is optional, and he can choose to reverse any one of the strings, or leave both of them as it is.)

Step 2 - He replaces the first character of both the current strings to 't'. This operation is compulsory.

He wins if he is able to make both the strings same (i.e. an exact match) after doing these two steps. Given the strings a and b, tell if thef can win or not!

Input: The first line of input contains T - The number of test cases in the file. The first and only line of every test case contains the 2 strings, a and b separated by a single space.

Output: For each test case, print Yes if thef wins, else print No

Constraints 1≤T≤104 1≤|a|,|b|≤100, where |a| refers to the length of string a. a and b will have ONLY lower case English alphabets. Subtasks 100 points- Original Constraints.

Sample Input:

3

man nam

abcd ebcd

abd ebf

Sample Output:

Yes

Yes

No

EXPLANATION: For the first test case, you can reverse man to nam and then replace both first characters of both strings to get tam. You could also have reversed nam to man, and then replaced the first two characters to get tan. Either of these two methods leads thef to win the game. For the second test case, you can simply skip reversing any of the strings and just replace the first characters by t to get both the strings as tbcd. The 2 strings cannot be made equal in the third test case.

My code is:

#include <iostream>
using namespace std;
#include <bits/stdc  .h>
#include<string.h>

int main() {
    int test;
    cin>>test;
    while(test--){
        string str1{},str2{},str3{};
        cin>>str1>>str2;
        str3=str2;
        str1[0]='t';
        str3[0]='t';
        if(str1==str3){
            cout<<"Yes"<<endl;
        }
        else{
            int len=str2.length();
            int n=len-1;
            for(int i=0;i<len/2;i  ){
                swap(str2[i],str2[n]);
                n=n-1;
            }
            str2[0]='t';
            if(str2==str1){
                cout<<"Yes"<<endl;
            }
            else{
                cout<<"No"<<endl;
            }
        }
    }
    return 0;
}

Now when i run it with the test case provided, i get the expected output. But since im doing this problem on codechef, when i submit it, it runs its own test cases(which is not visible) which my code does not pass. I have tried thinking alot on what possible case i am not considering . Please help me solve this problem.

CodePudding user response:

There is no need to insert the letter 't' to determine that two strings can coincide.

It is enough to write the if statement

#include <iterator>
#include <algorithm>

//...

auto win = []( const auto &s1, const auto &s2 )
{
    return ( std::size( s1 ) == std::size( s2 ) ) &&
           ( std::equal( std::next( std::begin( s1 ) ), std::end( s1 ),
                         std::next( std::begin( s2 ) ) ) ||
             std::equal( std::next( std::begin( s1 ) ), std::end( s1 ),
                         std::next( std::rbegin( s2 ) ) ) );
};

if ( win( s1, s2 ) )
{
    std::cout << "Yes\n";
}
else
{
    std::cout << "No\n";
}

That is at first you need to compare that two strings have the same length

std::size( s1 ) == std::size( s2 )

The above record can be also written like

s1.sisze() == s2.size()

Then you need to compare the two strings starting from their second characters using the standard algorithm std::equal This exapression

std::next( std::begin( s1 ) )

positions the iterator to the second character of the string.

And if this condition will return false you should compare the first string starting from its second character with the second string in the reverse order. This expression

std::next( std::rbegin( s2 ) )

position the reverse iterator of the second string to the second character of the string starting from its end.

This compound condition is written as a lambda expression that is called in the if statement.

Here is a demonstration program.

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>

int main()
{
    auto win = []( const auto &s1, const auto &s2 )
    {
        return ( std::size( s1 ) == std::size( s2 ) ) &&
                 ( std::equal( std::next( std::begin( s1 ) ), std::end( s1 ),
                               std::next( std::begin( s2 ) ) ) ||
                   std::equal( std::next( std::begin( s1 ) ), std::end( s1 ),
                               std::next( std::rbegin( s2 ) ) ) );
    };

    std::string s1( "abcd" ), s2( "ebcd" );

    if ( win( s1, s2 ) )
    {
        std::cout << "Yes\n";
    }
    else
    {
        std::cout << "No\n";
    } 

    s1 = "man"; s2 =  "nam";       

    if ( win( s1, s2 ) )
    {
        std::cout << "Yes\n";
    }
    else
    {
        std::cout << "No\n";
    } 

    s1 = "abd"; s2 = "ebf";

    if ( win( s1, s2 ) )
    {
        std::cout << "Yes\n";
    }
    else
    {
        std::cout << "No\n";
    } 
}

The program output is

Yes
Yes
No
  • Related