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.
Input: str = “UPBRC”
UP, PB, BR and RC are 4 different state codes that appear in string as contiguous sub-strings.
Input: str = “UPUP”
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:
- Find distinct characters in distinct substrings of a string
- Minimum changes to a string to make all substrings distinct
- Count of distinct substrings of a string using Suffix Array
- Count of distinct substrings of a string using Suffix Trie
- Count distinct substrings that contain some characters at most k times
- Count number of distinct substrings of a given length
- Count number of substrings with exactly k distinct characters
- Final state of the string after modification
- Maximum contiguous 1 possible in a binary string after k rotations
- Number of substrings of a string
- Replace two substrings (of a string) with each other
- Different substrings in a string that start and end with given strings
- Lexicographical concatenation of all substrings of a string
- Program to print all substrings of a given string
- Sum of all substrings of a string representing a number | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.