📜  将中间字符串解码为原始字符串

📅  最后修改于: 2022-05-13 01:57:08.829000             🧑  作者: Mango

将中间字符串解码为原始字符串

给定一个以中间形式写的字符串s,将其改回原始字符串。字符串字符串的字母。如果字符串的长度是偶数,则中间字母是两个中间字母的左边。给定的字符串是通过写下单词的中间字母,然后将其删除并重复该过程直到没有字母留下来形成的。

例子:

Input: eekgs
Output: geeks 
Explanation: in the original string “geeks” 
can be written in median form by picking up 
e first then, again e, then k then g and at
the end s. As these are the median when the
median letter is picked and deleted. 

Input: abc 
Output: bac 
Explanation: median of bac is a, then median
of bc is b, then median of c is c.  

为了找到答案,我们可以从左到右遍历给定的编码字符串,并添加答案字符串中的每个字母,一个字母到开头,下一个字母到结尾,下一个字母开始,等等。如果 n 是偶数,则必须将第一个字母添加到开头,将第二个字母添加到末尾。在另一种情况下,第一个字母到结尾,第二个到开头。我们需要做到这一点,直到我们不添加给定字符串中的所有字母。
注意:对于偶数长度的字符串,当我们将第一个字符添加到开头并将第二个字符添加到末尾时,剩余的字符串将始终是偶数长度。对于奇数长度的字符串也是如此。
下面给出的是上述方法的实现

C++
// C++ program to decode a median string
// to the original string
 
#include 
using namespace std;
 
// function to calculate the median back string
string decodeMedianString(string s)
{
    // length of string
    int l = s.length();
 
    // initialize a blank string
    string s1 = "";
 
    // Flag to check if length is even or odd
    bool isEven = (l % 2 == 0)? true : false;
 
    // traverse from first to last
    for (int i = 0; i < l; i += 2) {
 
        // if len is even then add first character
        // to beginning of new string and second
        // character to end
        if (isEven) {  
            s1 = s[i] + s1;
            s1 += s[i + 1];
        } else {
 
            // if current length is odd and is
            // greater than 1
            if (l - i > 1) {
 
                // add first character to end and
                // second character to beginning
                s1 += s[i];
                s1 = s[i + 1] + s1;
            } else {
 
                // if length is 1, add character 
                // to end
                s1 += s[i];
            }
        }
    }
 
    return s1;
}
 
// driver program
int main()
{
    string s = "eekgs";
    cout << decodeMedianString(s);
    return 0;
}


Java
// java program to decode a median
// string to the original string
 
public class GFG {
     
    // function to calculate the
    // median back string
    static String decodeMedianString(String s)
    {
         
        // length of string
        int l = s.length();
     
        // initialize a blank string
        String s1 = "";
     
        // Flag to check if length is
        // even or odd
        boolean isEven = (l % 2 == 0) ?
                          true : false;
     
        // traverse from first to last
        for (int i = 0; i < l; i += 2)
        {
     
            // if len is even then add
            // first character to
            // beginning of new string
            // and second character to
            // end
            if (isEven) {
                s1 = s.charAt(i) + s1;
                s1 += s.charAt(i+1);
            }
            else {
     
                // if current length is
                // odd and is greater
                // than 1
                if (l - i > 1) {
     
                    // add first character
                    // to end and second
                    // character to
                    // beginning
                    s1 += s.charAt(i);
                    s1 = s.charAt(i+1) + s1;
                }
                else {
     
                    // if length is 1,
                    // add character
                    // to end
                    s1 += s.charAt(i);
                }
            }
        }
     
        return s1;
    }
     
    // Driver code
    public static void main(String args[])
    {
        String s = "eekgs";
         
        System.out.println(
                    decodeMedianString(s));
    }
}
 
// This code is contributed by Sam007.


Python3
# Python3 program to decode a median
# string to the original string
 
# function to calculate the median
# back string
def decodeMedianString(s):
     
    # length of string
    l = len(s)
 
    # initialize a blank string
    s1 = ""
 
    # Flag to check if length is
    # even or odd
    if(l % 2 == 0):
        isEven = True
    else:
        isEven = False
 
    # traverse from first to last
    for i in range(0, l, 2):
         
        # if len is even then add first
        # character to beginning of new
        # string and second character to end
        if (isEven):
            s1 = s[i] + s1
            s1 += s[i + 1]
        else :
             
            # if current length is odd and
            # is greater than 1
            if (l - i > 1):
                 
                # add first character to end and
                # second character to beginning
                s1 += s[i]
                s1 = s[i + 1] + s1
            else:
                 
                # if length is 1, add character
                # to end
                s1 += s[i]
 
    return s1
 
# Driver Code
if __name__ == '__main__':
    s = "eekgs"
    print(decodeMedianString(s))
 
# This code is contributed by
# Sanjit_Prasad


C#
// C# program to decode a median
// string to the original string
using System;
 
class GFG {
     
    // function to calculate the
    // median back string
    static string decodeMedianString(string s)
    {
         
        // length of string
        int l = s.Length;
     
        // initialize a blank string
        string s1 = "";
     
        // Flag to check if length is
        // even or odd
        bool isEven = (l % 2 == 0) ?
                         true : false;
     
        // traverse from first to last
        for (int i = 0; i < l; i += 2)
        {
     
            // if len is even then add
            // first character to
            // beginning of new string
            // and second character to
            // end
            if (isEven) {
                s1 = s[i] + s1;
                s1 += s[i + 1];
            }
            else {
     
                // if current length is
                // odd and is greater
                // than 1
                if (l - i > 1) {
     
                    // add first character
                    // to end and second
                    // character to
                    // beginning
                    s1 += s[i];
                    s1 = s[i + 1] + s1;
                }
                else {
     
                    // if length is 1,
                    // add character
                    // to end
                    s1 += s[i];
                }
            }
        }
     
        return s1;
    }
 
    // Driver code
    public static void Main ()
    {
        string s = "eekgs";
        Console.WriteLine(
               decodeMedianString(s));
    }
}
 
// This code is contributed by Sam007.


Javascript


输出:

geeks