Encrypted Love 2.O

Word will start with the middle character of the string and will be formed henceforth with the middle characters of the right and left substrings (of the middle character of the word) and so on.

Example:

Input:  n=3, s="abc"
Output: bca

Approach

C++

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

string encryptedLove(string s)
{
    int mid;
    if (s.size() & 1)
        mid = s.size() / 2;
    else
        mid = s.size() / 2 - 1;
    if (mid == 0)
        return s.substr(mid);
    return s[mid] + encryptedLove(s.substr(mid + 1)) +
 encryptedLove(s.substr(0mid));
}
int main()
{

    int n = 3;

    string s = "abc";
    cout << encryptedLove(s<< "\n";

    return 0;
}


No comments:

Post a Comment