Distinct state codes that appear in a string as contiguous sub | C # 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
using System ; using System . Collections . Generic ; class GFG { static int countDistinctCode ( String str ) { HashSet < String > codes = new HashSet < String > ( ) ; for ( int i = 0 ; i < str . Length - 1 ; i ++ ) codes . Add ( str . Substring ( i , 2 ) ) ; return codes . Count ; } public static void Main ( String [ ] args ) { String str = " UPUP " ; Console . Write ( countDistinctCode ( str ) ) ; } }