Generate two output strings depending upon occurrence of character in input string . | Java program to print two strings made of character occurring once and multiple times ; function to print two strings generated from single string one with characters occurring onces other with character occurring multiple of times ; initialize hashtable with zero entry ; perform hashing for input string ; generate string ( str1 ) consisting char occurring once and string ( str2 ) consisting char occurring multiple times ; print both strings ; driver program
class GFG { final static int MAX_CHAR = 256 ; static void printDuo ( String str ) { int countChar [ ] = new int [ MAX_CHAR ] ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { countChar [ str . charAt ( i ) - ' a ' ] ++ ; } String str1 = " " , str2 = " " ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( countChar [ i ] > 1 ) { str2 = str2 + ( char ) ( i + ' a ' ) ; } else if ( countChar [ i ] == 1 ) { str1 = str1 + ( char ) ( i + ' a ' ) ; } } System . out . print ( " String ▁ with ▁ characters ▁ occurring ▁ " + "once:NEW_LINE"); System . out . print ( str1 + "NEW_LINE"); System . out . print ( " String ▁ with ▁ characters ▁ occurring ▁ " + "multiple times:NEW_LINE"); System . out . print ( str2 + "NEW_LINE"); System . out . print ( " " ) ; } public static void main ( String [ ] args ) { String str = " lovetocode " ; printDuo ( str ) ; } }