title: how to compare two strings to find common substring
body: <p>i get termination due to timeout error when i compile. Please help me</p>
  
  <p>Given two strings, determine if they share a common substring. A substring may be as small as one character.</p>
  
  <p>For example, the words "a", "and", "art" share the common substring "a" . The words "be" and "cat" do not share a substring.</p>
  
  <p><strong>Input Format</strong></p>
  
  <p>The first line contains a single integer , the number of test cases.</p>
  
  <p>The following  pairs of lines are as follows:</p>
  
  <p>The first line contains string s1 .
  The second line contains string s2 .</p>
  
  <p><strong>Output Format</strong></p>
  
  <p>For each pair of strings, return YES or NO.</p>
  
  <p><strong>my code in java</strong></p>
  
  <pre><code>public static void main(String args[])
  {
      String s1,s2;
      int n;
      Scanner s= new Scanner(System.in);
      n=s.nextInt();
      while(n&gt;0)
      {    
      int flag = 0;
          s1=s.next();
  
      s2=s.next();
      for(int i=0;i&lt;s1.length();i++)
      {
          for(int j=i;j&lt;s2.length();j++)
          {
              if(s1.charAt(i)==s2.charAt(j))
              {
                  flag=1;
              }
          }
      }
  
  
          if(flag==1)
          {
              System.out.println("YES");
          }
          else
          {
              System.out.println("NO");
          }
          n--;
  }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>any tips?</p>