Check if two strings have a common substring | Java program to check if two strings have common substring ; function to return true if strings have common substring and no if strings have no common substring ; vector for storing character occurrences ; increment vector index for every character of str1 ; checking common substring of str2 in str1 ; Driver code
import java . util . Arrays ; class GFG { static int MAX_CHAR = 26 ; static boolean twoStrings ( String s1 , String s2 ) { boolean v [ ] = new boolean [ MAX_CHAR ] ; Arrays . fill ( v , false ) ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) v [ s1 . charAt ( i ) - ' a ' ] = true ; for ( int i = 0 ; i < s2 . length ( ) ; i ++ ) if ( v [ s2 . charAt ( i ) - ' a ' ] ) return true ; return false ; } 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 " ) ; } }