Distinct state codes that appear in a string as contiguous sub | C ++ implementation of the 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDistinctCode ( string str ) { set < string > codes ; for ( int i = 0 ; i < str . length ( ) - 1 ; i ++ ) codes . insert ( str . substr ( i , 2 ) ) ; return codes . size ( ) ; } int main ( ) { string str = " UPUP " ; cout << countDistinctCode ( str ) ; return 0 ; }