Given two strings, and , determine if they share a common substring.
Input Format
The first line contains a single integer, , denoting the number of pairs you must check.
Each pair is defined over two lines:
Each pair is defined over two lines:
- The first line contains string .
- The second line contains string .
Constraints
- and consist of lowercase English letters only.
Output Format
For each pair of strings, print
YES
on a new line if the two strings share a common substring; if no such common substring exists, print NO
on a new line.
Sample Input
2
hello
world
hi
world
Sample Output
YES
NO
Explanation
We have pairs to check:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static String twoStrings(String s1, String s2){
// Complete this function
for(int i='a';i<='z';i++)
if(s1.indexOf(i)!=-1&&s2.indexOf(i)!=-1)
return "YES" ;
return "NO" ;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
for(int a0 = 0; a0 < q; a0++){
String s1 = in.next();
String s2 = in.next();
String result = twoStrings(s1, s2);
System.out.println(result);
}
}
}
- , . The substrings and are common to both and , so we print
YES
on a new line. - , . Because and have no common substrings, we print
NO
on a new line.
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static String twoStrings(String s1, String s2){
// Complete this function
for(int i='a';i<='z';i++)
if(s1.indexOf(i)!=-1&&s2.indexOf(i)!=-1)
return "YES" ;
return "NO" ;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
for(int a0 = 0; a0 < q; a0++){
String s1 = in.next();
String s2 = in.next();
String result = twoStrings(s1, s2);
System.out.println(result);
}
}
}
No comments:
Post a Comment