Compruebe si hay algún carácter común en dos strings dadas

Dadas dos strings. La tarea es verificar que haya algún carácter común entre dos strings. 

Ejemplos: 

Input: s1 = "geeksforgeeks", s2 = "geeks"
Output: Yes

Input: s1 = "geeks", s2 = "for"
Output: No

Enfoque: Atraviese la 1ra string y mapee los caracteres de la string con su frecuencia, en este mapa los caracteres actúan como una clave y la frecuencia su valor. Luego, recorra la segunda string y verificaremos si hay algún carácter que esté presente en ambas strings, luego se confirma que hay una subsecuencia común.

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

C++

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to match character
bool check(string s1, string s2)
{
    // Create a map to map
    // characters of 1st string
    map<char, int> map;
 
    // traverse the first string
    // and create a hash map
    for (int i = 0; i < s1.length(); i++)
        map[s1[i]]++;
 
    // traverse the second string
    // and if there is any
    // common character than return 1
    for (int i = 0; i < s2.length(); i++)
        if (map[s2[i]] > 0)
            return true;
 
    // else return 0
    return false;
}
 
// Driver code
int main()
{
    // Declare two strings
    string s1 = "geeksforgeeks", s2 = "geeks";
 
    // Find if there is a common subsequence
    bool yes_or_no = check(s1, s2);
 
    if (yes_or_no == true)
        cout << "Yes" << endl;
 
    else
        cout << "No" << endl;
 
    return 0;
}

Java

// Java implementation of above approach
import java.util.*;
 
class GFG
{
 
// Function to match character
static boolean check(String s1, String s2)
{
    // Create a map to map
    // characters of 1st string
    Map<Character, Integer> mp = new HashMap<>();
 
    // traverse the first string
    // and create a hash map
    for (int i = 0; i < s1.length(); i++)
    {
        mp.put(s1.charAt(i), mp.get(s1.charAt(i)) == null ? 1 : mp.get(s1.charAt(i)) + 1);
    }
 
    // traverse the second string
    // and if there is any
    // common character than return 1
    for (int i = 0; i < s2.length(); i++)
    {
        if (mp.get(s2.charAt(i)) > 0)
        {
            return true;
        }
    }
 
    // else return 0
    return false;
}
 
// Driver code
public static void main(String[] args)
{
    // Declare two strings
    String s1 = "geeksforgeeks", s2 = "geeks";
 
    // Find if there is a common subsequence
    boolean yes_or_no = check(s1, s2);
 
    if (yes_or_no == true)
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
/* This code contributed by PrinciRaj1992 */

Python3

# Python3 program to check whether
# two lists are overlapping or not
def is_member(List, key):
 
    for i in range(0, len(List)):
        if key == List[i]:
            return True
    return False
 
def overlap(List1 , List2):
 
    for key in List1:
        if is_member( List2, key ):
            return True
 
    return False
 
# Driver Code
if __name__ == '__main__':
 
    s1 = 'geeksforgeeks'
    s2 = 'geeks'
 
    List1 = list( s1 )
    List2 = list( s2 )
 
    yes_or_no = str(overlap( List1, List2 ))
     
    if (yes_or_no):
        print("Yes")
    else:
        print("No")
 
# This code is contributed
# by Krishna_Yadav

C#

// C# program to check if successive
// pair of numbers in the queue are
// consecutive or not
using System;
using System.Collections.Generic;
 
class GFG
{
 
// Function to match character
static Boolean check(String s1, String s2)
{
    // Create a map to map
    // characters of 1st string
    Dictionary<char,int> mp = new Dictionary<char,int>();
 
    // traverse the first string
    // and create a hash map
    for (int i = 0; i < s1.Length; i++)
    {
        if(mp.ContainsKey(s1[i]))
        {
            var val = mp[s1[i]];
            mp.Remove(s1[i]);
            mp.Add(s1[i], val + 1);
        }
        else
        {
            mp.Add(s1[i], 1);
        }
    }
 
    // traverse the second string
    // and if there is any
    // common character than return 1
    for (int i = 0; i < s2.Length; i++)
    {
        if (mp[s2[i]] > 0)
        {
            return true;
        }
    }
 
    // else return 0
    return false;
}
 
// Driver code
public static void Main(String[] args)
{
    // Declare two strings
    String s1 = "geeksforgeeks", s2 = "geeks";
 
    // Find if there is a common subsequence
    Boolean yes_or_no = check(s1, s2);
 
    if (yes_or_no == true)
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code contributed by Rajput-Ji

Javascript

<script>
 
// Javascript implementation of above approach
 
// Function to match character
function check( s1, s2)
{
    // Create a map to map
    // characters of 1st string
    var map = new Map();
 
    // traverse the first string
    // and create a hash map
    for (var i = 0; i < s1.length; i++)
    {
        if(map.has(s1[i].charCodeAt(0)))
        {
            map[s1[i].charCodeAt(0)]++;
        }
        else
        {
            map[s1[i].charCodeAt(0)]=1;
        }
         
    }
 
    // traverse the second string
    // and if there is any
    // common character than return 1
    for (var i = 0; i < s2.length; i++)
        if (map[s2[i].charCodeAt(0)] > 0)
            return true;
 
    // else return 0
    return false;
}
 
// Driver code
// Declare two strings
var s1 = "geeksforgeeks", s2 = "geeks";
// Find if there is a common subsequence
var yes_or_no = check(s1, s2);
if (yes_or_no)
    document.write( "Yes");
else
    document.write( "No" );
 
</script>
Producción: 

Yes

 

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

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 *