Cree una nueva string combinando alternativamente los caracteres de dos mitades de la string al revés

Dada una string s, cree una nueva string tal que contenga los caracteres de las dos mitades de la string s combinados alternativamente en orden inverso. 
Ejemplos: 
 

Input : s = carbohydrates
Output : hsoebtraarcdy

Input : s = sunshine
Output : sennuish

Explicación:  
Ejemplo 1: Dos mitades de la string de carbohidratos son carbohidratos e hidratos . Como debían agregarse al revés alternativamente, comience con h de la primera mitad, luego s de la segunda mitad, seguido de o de la primera mitad, e de la segunda mitad y así sucesivamente. La string p resulta ser hsoebtraarcdy . Si una de las strings está completamente terminada, simplemente agregue los caracteres restantes de la otra string en orden inverso. 
Ejemplo 2: Las dos mitades de la cuerda son soles y hine . String sensuales la string deseada p
 

C++

// C++ program for creating a string
// by alternately combining the
// characters of two halves
// in reverse
#include <bits/stdc++.h>
using namespace std;
 
// Function performing calculations
void solve(string s)
{
    int l = s.length();
    int x = l / 2;
    int y = l;
     
    // Calculating the two halves
    // of string s as first and
    // second. The final string p
    string p = "";
    while (x > 0 && y > l / 2) {
         
        // It joins the characters to
        // final string in reverse order
        p += s[x - 1];
        x--;
         
        // It joins the characters to
        // final string in reverse order
        p += s[y - 1];
        y--;
    }
     
    if (y > l / 2) {
        p += s[y - 1];
        y--;
    }
     
    cout << p;
}
 
// Driver code
int main()
{
    string s = "sunshine";
     
    // Calling function
    solve(s);
    return 0;
}

Java

// Java program for creating a string
// by alternately combining the
// characters of two halves
// in reverse
import java.io.*;
 
class GFG {
     
    // Function performing calculations
    public static void solve(String s)
    {
        int l = s.length();
        int x = l / 2;
        int y = l;
     
        // Calculating the two halves of
        // string s as first and second
        // The final string p
        String p = "";
        while (x > 0 && y > l / 2) {
         
            // It joins the characters to
            // final string in reverse order
            char ch = s.charAt(x - 1);
            p += ch;
            x--;
             
            // It joins the characters to
            // final string in reverse order
            ch = s.charAt(y - 1);
            p += ch;
            y--;
        }
         
        if (y > l / 2) {
            char ch = s.charAt(x - 1);
            p += ch;
            y--;
        }
        System.out.println(p);
    }
     
    // Driver method
    public static void main(String args[])
    {
        String s = "sunshine";
         
        // Calling function
        solve(s);
    }
}

Python3

# Python 3 program for creating a string
# by alternately combining the
# characters of two halves
# in reverse
 
# Function performing calculations
def solve(s) :
    l = len(s)
    x = l // 2
    y = l
      
    # Calculating the two halves
    # of string s as first and
    # second. The final string p
    p = ""
    while (x > 0 and y > l / 2) :
 
        # It joins the characters to
        # final string in reverse order
        p =  p + s[x - 1]
        x = x - 1
          
        # It joins the characters to
        # final string in reverse order
        p = p + s[y - 1]
        y = y - 1
     
      
    if (y > l // 2) :
        p = p + s[y - 1]
        y = y - 1
     
    print (p)
 
# Driver code
s = "sunshine"
 
# Calling function
solve(s)
 
 
# This code is contributed by Nikita Tiwari

C#

// C# program for creating a string
// by alternately combining the
// characters of two halves
// in reverse
using System;
 
class GFG {
     
    // Function performing calculations
    public static void solve(string s)
    {
        int l = s.Length;
        int x = l / 2;
        int y = l;
     
        // Calculating the two halves of
        // string s as first and second
        // The final string p
        string p = "";
        while (x > 0 && y > l / 2) {
         
            // It joins the characters to
            // final string in reverse order
            char ch = s[x - 1];
            p += ch;
            x--;
             
            // It joins the characters to
            // final string in reverse order
            ch = s[y - 1];
            p += ch;
            y--;
        }
         
        if (y > l / 2)
        {
            char ch = s[x - 1];
            p += ch;
            y--;
        }
        Console.WriteLine(p);
    }
     
    // Driver method
    public static void Main()
    {
        string s = "sunshine";
         
        // Calling function
        solve(s);
    }
}
// This code is contributed by vt_m.

PHP

<?php
// Python 3 program for creating a string
// by alternately combining the
// characters of two halves in reverse
 
// Function performing calculations
function solve($s)
{
    $l = strlen($s);
    $x = $l / 2;
    $y = $l;
     
    // Calculating the two halves
    // of string s as first and
    // second. The final string p
    $p = "";
    while ($x > 0 && $y > $l / 2)
    {
         
        // It joins the characters to
        // final string in reverse order
        $p = $p.$s[$x - 1];
        $x--;
         
        // It joins the characters to
        // final string in reverse order
        $p = $p.$s[$y - 1];
        $y--;
    }
     
    if ($y > $l / 2)
    {
        $p = $p.$s[$y - 1];
        $y--;
    }
     
    echo $p;
}
 
// Driver code
$s = "sunshine";
     
// Calling function
solve($s);
 
// This code is contributed
// by ChitraNayal
?>

Javascript

<script>
 
// JavaScript program for creating a string
// by alternately combining the
// characters of two halves
// in reverse
 
    // Function performing calculations
    function solve( s) {
        var l = s.length;
        var x = l / 2;
        var y = l;
 
        // Calculating the two halves of
        // string s as first and second
        // The final string p
        var p = "";
        while (x > 0 && y > l / 2) {
 
            // It joins the characters to
            // final string in reverse order
            var ch = s.charAt(x - 1);
            p += ch;
            x--;
 
            // It joins the characters to
            // final string in reverse order
            ch = s.charAt(y - 1);
            p += ch;
            y--;
        }
 
        if (y > l / 2) {
            var ch = s.charAt(x - 1);
            p += ch;
            y--;
        }
        document.write(p);
    }
 
    // Driver method
     
        var s = "sunshine";
 
        // Calling function
        solve(s);
 
 
// This code is contributed by todaysgaurav
 
</script>

Producción: 

sennuish

Complejidad de tiempo : O(n) donde n es la longitud de la string

Espacio Auxiliar: O(n)
 

Publicación traducida automáticamente

Artículo escrito por HGaur y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *