Se le dan dos strings str1 y str2. Debe verificar si las dos strings comparten una substring común.
Ejemplos:
Input : str1 = "HELLO" str2 = "WORLD" Output : YES Explanation : The substrings "O" and "L" are common to both str1 and str2 Input : str1 = "HI" str2 = "ALL" Output : NO Explanation : Because str1 and str2 have no common substrings
C++
// CPP program to check if two strings have // common substring #include <bits/stdc++.h> using namespace std; const int MAX_CHAR = 26; // function to return true if strings have // common substring and no if strings have // no common substring bool twoStrings(string s1, string s2) { // vector for storing character occurrences vector<bool> v(MAX_CHAR, 0); // increment vector index for every // character of str1 for (int i = 0; i < s1.length(); i++) v[s1[i] - 'a'] = true; // checking common substring of str2 in str1 for (int i = 0; i < s2.length(); i++) if (v[s2[i] - 'a']) return true; return false; } // driver program int main() { string str1 = "hello"; string str2 = "world"; if (twoStrings(str1, str2)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java program to check if two strings have // common substring import java.util.Arrays; class GFG { static int MAX_CHAR = 26; // function to return true if strings have // common substring and no if strings have // no common substring static boolean twoStrings(String s1, String s2) { // vector for storing character occurrences boolean v[]=new boolean[MAX_CHAR]; Arrays.fill(v,false); // increment vector index for every // character of str1 for (int i = 0; i < s1.length(); i++) v[s1.charAt(i) - 'a'] = true; // checking common substring of str2 in str1 for (int i = 0; i < s2.length(); i++) if (v[s2.charAt(i) - 'a']) return true; return false; } // Driver code public static void main (String[] args) { String str1 = "hello"; String str2 = "world"; if (twoStrings(str1, str2)) System.out.print("Yes"); else System.out.print("No"); } } // This code is contributed by Anant Agarwal.
C#
// C# program to check if two // strings have common substring using System; class GFG { static int MAX_CHAR = 26; // function to return true if strings have // common substring and no if strings have // no common substring static bool twoStrings(String s1, String s2) { // vector for storing character occurrences bool []v = new bool[MAX_CHAR]; // Arrays.fill(v,false); for(int i = 0; i < MAX_CHAR; i++) v[i]=false; // increment vector index for // every character of str1 for (int i = 0; i < s1.Length; i++) v[s1[i] - 'a'] = true; // checking common substring of str2 in str1 for (int i = 0; i < s2.Length; i++) if (v[s2[i] - 'a']) return true; return false; } // Driver code public static void Main () { String str1 = "hello"; String str2 = "world"; if (twoStrings(str1, str2)) Console.Write("Yes"); else Console.Write("No"); } } // This code is contributed by nitin mittal.
Javascript
<script> // javascript program to check if two strings have // common substring var MAX_CHAR = 26; // function to return true if strings have // common substring and no if strings have // no common substring function twoStrings(s1, s2) { // vector for storing character occurrences var v = Array(MAX_CHAR).fill(0); // increment vector index for every // character of str1 for (var i = 0; i < s1.length; i++) v[s1[i] - 'a'] = true; // checking common substring of str2 in str1 for (var i = 0; i < s2.length; i++) if (v[s2[i] - 'a']) return true; return false; } // driver program var str1 = "hello"; var str2 = "world"; if (twoStrings(str1, str2)) document.write( "Yes"); else document.write("No"); // This code is contributed by rutvik_56. </script>
Publicación traducida automáticamente
Artículo escrito por Shivam.Pradhan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA