Distinct state codes that appear in a string as contiguous sub | Java implementation of the above approach . ; Function to return the count of distinct state codes ; Insert every sub - String of length 2 in the set ; Return the size of the set ; Driver code
import java . util . * ; class GFG { static int countDistinctCode ( String str ) { Set < String > codes = new HashSet < > ( ) ; for ( int i = 0 ; i < str . length ( ) - 1 ; i ++ ) codes . add ( str . substring ( i , i + 2 ) ) ; return codes . size ( ) ; } public static void main ( String [ ] args ) { String str = " UPUP " ; System . out . println ( countDistinctCode ( str ) ) ; } }