Code For Alls..!

get the code!!

Saturday, 22 July 2017

Two Strings

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:
  1. The first line contains string .
  2. 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:
  1. , . The substrings and are common to both and , so we print YES on a new line.
  2. , . Because and have no common substrings, we print NO on a new line.
Java code:
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