Every state is represented by string of length 2. For example **DL** is used for **Delhi**, **HP** for **Himachal Pradesh**, **UP** for **Uttar Pradesh**, **PB** for **Punjab** etc.

Given a string **str** consisting of uppercase English alphabets only, the task is to find the number of distinct state codes that appear in the string as contiguous sub-strings.

**Examples:**

Input:str = “UPBRC”

Output:4

UP, PB, BR and RC are 4 different state codes that appear in string as contiguous sub-strings.

Input:str = “UPUP”

Output:2

UP and PU are the only state codes that appear in the given string.

**Approach:** Store every sub-string of length 2 in a set and finally return the size of the set which is the required number of distinct state codes appearing in the given string as sub-strings.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count of ` `// distinct state codes ` `int` `countDistinctCode(string str) ` `{ ` ` ` `set<string> codes; ` ` ` `for` `(` `int` `i = 0; i < str.length() - 1; i++) ` ` ` ` ` `// Insert every sub-string ` ` ` `// of length 2 in the set ` ` ` `codes.insert(str.substr(i, 2)); ` ` ` ` ` `// Return the size of the set ` ` ` `return` `codes.size(); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str = ` `"UPUP"` `; ` ` ` `cout << countDistinctCode(str); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

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

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count of ` `# distinct state codes ` `def` `countDistinctCode(string): ` ` ` ` ` `codes ` `=` `set` `() ` ` ` `for` `i ` `in` `range` `(` `0` `, ` `len` `(string) ` `-` `1` `): ` ` ` ` ` `# Insert every sub-string ` ` ` `# of length 2 in the set ` ` ` `codes.add(string[i:i ` `+` `2` `]) ` ` ` ` ` `# Return the size of the set ` ` ` `return` `len` `(codes) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `string ` `=` `"UPUP"` ` ` `print` `(countDistinctCode(string)) ` ` ` `# This code is contributed ` `# by Rituraj Jain ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach. ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the count of ` `// distinct state codes ` `static` `int` `countDistinctCode(String str) ` `{ ` ` ` `HashSet<String> codes = ` `new` `HashSet<String>(); ` ` ` `for` `(` `int` `i = 0; i < str.Length - 1; i++) ` ` ` ` ` `// Insert every sub-String ` ` ` `// of length 2 in the set ` ` ` `codes.Add(str.Substring(i,2)); ` ` ` ` ` `// Return the size of the set ` ` ` `return` `codes.Count; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `String str = ` `"UPUP"` `; ` ` ` `Console.Write(countDistinctCode(str)); ` `} ` `} ` ` ` `// This code has been contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

**Output:**

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find distinct characters in distinct substrings of a string
- Contiguous subsegments of a string having distinct subsequent characters
- Contiguous unique substrings with the given length L
- Largest sub-string where all the characters appear at least K times
- Count of distinct substrings of a string using Suffix Trie
- Count of distinct substrings of a string using Suffix Array
- Generate a String of having N*N distinct non-palindromic Substrings
- Minimum changes to a string to make all substrings distinct
- Count distinct substrings of a string using Rabin Karp algorithm
- Count of Distinct Substrings occurring consecutively in a given String
- Count words that appear exactly two times in an array of words
- Check if all occurrences of a character appear together
- Remove characters that appear more than k times
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array
- Largest substring where all characters appear at least K times | Set 2
- Count of all possible Paths in a Tree such that Node X does not appear before Node Y
- Count number of substrings with exactly k distinct characters
- Count distinct substrings that contain some characters at most k times
- Count number of distinct substrings of a given length
- Count of substrings of length K with exactly K distinct characters

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.