Compruebe si dos strings contienen los mismos caracteres en el mismo orden

Dadas dos strings s1 y s2 , la tarea es encontrar si las dos strings contienen los mismos caracteres que aparecen en el mismo orden. Por ejemplo: la string «Geeks» y la string «Geks» contienen los mismos caracteres en el mismo orden.

Ejemplos: 

Entrada: s1 = «Geeks», s2 = «Geks» 
Salida:

Entrada: s1 = “Arnab”, s2 = “Andrew” 
Salida: No 

Enfoque: Tenemos dos strings ahora tenemos que verificar si las strings contienen los mismos caracteres en el mismo orden. Entonces, reemplazaremos el elemento similar contiguo con un solo elemento, es decir, si tenemos «eee» , lo reemplazaremos con una sola «e» . Ahora comprobaremos que ambas strings son iguales o no. Si es igual, imprima , de lo contrario ,  No.

A continuación se muestra la implementación del enfoque anterior: 

C++

// C++ implementation of the approach
#include <iostream>
using namespace std;
 
string getString(char x)
{
    // string class has a constructor
    // that allows us to specify size of
    // string as first parameter and character
    // to be filled in given size as second
    // parameter.
    string s(1, x);
 
    return s;
}
 
// Function that returns true if
// the given strings contain same
// characters in same order
bool solve(string s1, string s2)
{
    // Get the first character of both strings
    string a = getString(s1[0]), b = getString(s2[0]);
 
    // Now if there are adjacent similar character
    // remove that character from s1
    for (int i = 1; i < s1.length(); i++)
        if (s1[i] != s1[i - 1]) {
            a += getString(s1[i]);
        }
 
    // Now if there are adjacent similar character
    // remove that character from s2
    for (int i = 1; i < s2.length(); i++)
        if (s2[i] != s2[i - 1]) {
            b += getString(s2[i]);
        }
 
    // If both the strings are equal
    // then return true
    if (a == b)
        return true;
 
    return false;
}
 
// Driver code
int main()
{
    string s1 = "Geeks", s2 = "Geks";
 
    if (solve(s1, s2))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}

Java

// Java implementation of the approach
class temp
{
static String getString(char x)
{
 
    // String class has a constructor
    // that allows us to specify size of
    // String as first parameter and character
    // to be filled in given size as second
    // parameter.
    String s = String.valueOf(x);
    return s;
}
 
// Function that returns true if
// the given Strings contain same
// characters in same order
static boolean solve(String s1, String s2)
{
    // Get the first character of both Strings
    String a = getString(s1.charAt(0)),
           b = getString(s2.charAt(0));
 
    // Now if there are adjacent similar character
    // remove that character from s1
    for (int i = 1; i < s1.length(); i++)
        if (s1.charAt(i) != s1.charAt(i - 1))
        {
            a += getString(s1.charAt(i));
        }
 
    // Now if there are adjacent similar character
    // remove that character from s2
    for (int i = 1; i < s2.length(); i++)
        if (s2.charAt(i) != s2.charAt(i - 1))
        {
            b += getString(s2.charAt(i));
        }
 
    // If both the Strings are equal
    // then return true
    if (a.equals(b))
        return true;
 
    return false;
}
 
// Driver code
public static void main(String[] args)
{
    String s1 = "Geeks", s2 = "Geks";
 
    if (solve(s1, s2))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by ankush_953

Python3

# Python3 implementation of the approach
 
def getString(x):
 
    # string class has a constructor
    # that allows us to specify the size of
    # string as first parameter and character
    # to be filled in given size as the second
    # parameter.
    return x
 
# Function that returns true if
# the given strings contain same
# characters in same order
def solve(s1, s2):
 
    # Get the first character of both strings
    a = getString(s1[0])
    b = getString(s2[0])
 
    # Now if there are adjacent similar character
    # remove that character from s1
    for i in range(1, len(s1)):
        if s1[i] != s1[i - 1]:
            a += getString(s1[i])
         
    # Now if there are adjacent similar character
    # remove that character from s2
    for i in range(1, len(s2)):
        if s2[i] != s2[i - 1]:
            b += getString(s2[i])
         
    # If both the strings are equal
    # then return true
    if a == b:
        return True
    return False
 
# Driver code
s1 = "Geeks"
s2 = "Geks"
if solve(s1, s2):
    print("Yes")
else:
    print("No")
 
# This code is contributed by ankush_953

C#

// C# implementation of the approach
using System;
     
public class temp
{
     
static String getString(char x)
{
 
    // String class has a constructor
    // that allows us to specify size of
    // String as first parameter and character
    // to be filled in given size as second
    // parameter.
    String s = String.Join("",x);
    return s;
}
 
// Function that returns true if
// the given Strings contain same
// characters in same order
static Boolean solve(String s1, String s2)
{
    // Get the first character of both Strings
    String a = getString(s1[0]),
        b = getString(s2[0]);
 
    // Now if there are adjacent similar character
    // remove that character from s1
    for (int i = 1; i < s1.Length; i++)
        if (s1[i] != s1[i - 1]) {
            a += getString(s1[i]);
        }
 
    // Now if there are adjacent similar character
    // remove that character from s2
    for (int i = 1; i < s2.Length; i++)
        if (s2[i] != s2[i - 1]) {
            b += getString(s2[i]);
        }
 
    // If both the strings are equal
    // then return true
    if (a == b)
        return true;
 
    return false;
}
 
// Driver code
public static void Main(String[] args)
{
    String s1 = "Geeks", s2 = "Geks";
 
    if (solve(s1, s2))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by Princi Singh

Javascript

<script>
 
// Javascript implementation of the approach
 
function getString(x)
{
    // string class has a constructor
    // that allows us to specify size of
    // string as first parameter and character
    // to be filled in given size as second
    // parameter.
    return x
}
 
// Function that returns true if
// the given strings contain same
// characters in same order
function solve(s1, s2)
{
    // Get the first character of both strings
    var a = getString(s1[0]), b = getString(s2[0]);
 
    // Now if there are adjacent similar character
    // remove that character from s1
    for (var i = 1; i < s1.length; i++)
        if (s1[i] != s1[i - 1]) {
            a += getString(s1[i]);
        }
 
    // Now if there are adjacent similar character
    // remove that character from s2
    for (var i = 1; i < s2.length; i++)
        if (s2[i] != s2[i - 1]) {
            b += getString(s2[i]);
        }
 
    // If both the strings are equal
    // then return true
    if (a == b)
        return true;
 
    return false;
}
 
// Driver code
var s1 = "Geeks", s2 = "Geks";
if (solve(s1, s2))
    document.write( "Yes");
else
    document.write( "No");
 
 
// This code is contributed by rutvik_56.
</script>
Producción: 

Yes

 

Usando recursividad

C++

#include <iostream>
 
using namespace std;
bool checkSequence(string a, string b)
{
      // if length of the b = 0
      // then we return true
      if(b.size() == 0)
        return true;
   
      // if length of a = 0
      // that means b is not present in
      // a so we return false
    if(a.size() == 0)
        return false;
 
    if(a[0] == b[0])
        return checkSequence(a.substr(1), b.substr(1));
    else
        return checkSequence(a.substr(1), b);
}
 
int main()
{
    string s1 = "Geeks", s2 = "Geks";
 
    if (checkSequence(s1, s2))
        cout << "Yes";
    else
        cout << "No";
}
 
// This code is contributed by SoumikMondal

Java

/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
      public static boolean checkSequence(String a, String b) {
          //if length of the b = 0
          //then we return true
          if(b.length()==0)
            return true;
       
          //if length of a = 0
          //that means b is not present in
          //a so we return false
        if(a.length() == 0)
            return false;
         
        if(a.charAt(0) == b.charAt(0))
            return checkSequence(a.substring(1), b.substring(1));
        else
            return checkSequence(a.substring(1), b);
    }
    public static void main(String[] args)
    {
        String s1 = "Geeks", s2 = "Geks";
   
        if (checkSequence(s1, s2))
            System.out.print("Yes");
        else
            System.out.print("No");
    }
}

Python

# Python3 implementation of approach
def checkSequence(a,  b):
 
    # if length of the b = 0
    # then we return true
    if len(b) == 0:
        return True
 
    # if length of a = 0
    # that means b is not present in
    # a so we return false
    if len(a) == 0:
        return False
 
    if(a[0] == b[0]):
        return checkSequence(a[1:], b[1:])
    else:
        return checkSequence(a[1:], b)
 
 
if __name__ == '__main__':
    s1 = "Geeks"
    s2 = "Geks"
 
    if (checkSequence(s1, s2)):
        print("Yes")
    else:
        print("No")
 
 
# This code is contributed by nirajgusain5

C#

// C# implementation of the approach
using System;
     
public class temp
{
public static bool checkSequence(String a, String b)
{
   
          // if length of the b = 0
          // then we return true
          if(b.Length == 0)
            return true;
       
          // if length of a = 0
          // that means b is not present in
          // a so we return false
        if(a.Length == 0)
            return false;
         
        if(a[0] == b[0])
            return checkSequence(a.Substring(1), b.Substring(1));
        else
            return checkSequence(a.Substring(1), b);
    }
 
// Driver code
public static void Main(String[] args)
{
    String s1 = "Geeks", s2 = "Geks";
 
    if (checkSequence(s1, s2))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by Dharanendra L V.

Javascript

<script>
 
function checkSequence(a, b)
{
     
    // If length of the b = 0
    // then we return true
    if (b.length == 0)
        return true;
     
    // If length of a = 0
    // that means b is not present in
    // a so we return false
    if (a.length == 0)
        return false;
     
    if (a[0] == b[0])
        return checkSequence(a.substring(1),
                             b.substring(1));
    else
        return checkSequence(a.substring(1), b);
}
 
// Driver code
let s1 = "Geeks", s2 = "Geks";
 
if (checkSequence(s1, s2))
    document.write("Yes");
else
    document.write("No");
     
// This code is contributed by mukesh07
 
</script>

Producción:

Yes

Publicación traducida automáticamente

Artículo escrito por andrew1234 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 *