Possibility of a word from a given set of characters | java program to check if a query string is present is given set . ; Count occurrences of all characters in s . ; Check if number of occurrences of every character in q is less than or equal to that in s . ; driver program
import java . io . * ; public class GFG { static int MAX_CHAR = 256 ; static boolean isPresent ( String s , String q ) { int [ ] freq = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) freq [ s . charAt ( i ) ] ++ ; for ( int i = 0 ; i < q . length ( ) ; i ++ ) { freq [ q . charAt ( i ) ] -- ; if ( freq [ q . charAt ( i ) ] < 0 ) return false ; } return true ; } static public void main ( String [ ] args ) { String s = " abctd " ; String q = " cat " ; if ( isPresent ( s , q ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }