-2

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 I'm 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 a lot on what possible case i am not considering.

halfer
  • 19,824
  • 17
  • 99
  • 186
shaun
  • 13
  • 5
  • You're only reversing one of the strings (see `std::reverse` for an easier way to do this by the way) but you should consider the case of reversing the other string – Alan Birtles May 27 '22 at 18:03
  • @AlanBirtles i tried the reverse method, its giving me the same result. And im keeping one string constant and changing the other – shaun May 27 '22 at 18:08
  • e.g. the input of `abc cbf` gives `yes` but `cbf abc` gives `no` but I think both should be `yes` – Alan Birtles May 27 '22 at 18:14
  • @shaun _"Please help me solve this problem."_ I could start teaching you how to use your debugger efficiently, but that's way beyond the scope of this site I am afraid. – πάντα ῥεῖ May 27 '22 at 18:18
  • Has nobody mentioned `` yet? You really shouldn't use that, and you should regard any teaching resource that suggests you _should_ use it, with appropriate suspicion. – Useless May 27 '22 at 18:22
  • The combination of `#include ` and `#include ` suggests you do not understand what `#include ` does and how it should be used. [Here's a link to help with that](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – user4581301 May 27 '22 at 18:28
  • 2
    Please use useful and informative titles on Stack Overflow. – halfer May 28 '22 at 10:20
  • Also, think about what you're asking for: It's effectively asking people to guess what some external site may apply as testcase against your implementation. That's not a programming question worth archiving here on SO, even if it's programming-related and important to you currently. – Ulrich Eckhardt May 28 '22 at 10:29

1 Answers1

1

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
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335