Given a string containing the words and dates, the task is to find the total number of distinct years mentioned in that string.
Note: Assuming that the date will be in ‘DD-MM-YYYY’ format.
Input: str = "UN was established on 24-10-1945. India got freedom on 15-08-1947." Output: 2 2 distinct years i.e. 1945 and 1947 have been referenced. Input: str = "Soon after the world war 2 ended on 02-09-1945. The UN was established on 24-10-1945." Output: 1 Only 1 Year, i.e 1945 has been referenced .
- Start traversing the string.
- Check if the current character is a digit. Store it in another string i.e. dateString.
- Check if the current character is ‘-‘ then remove the characters stored in the dateString.
- Check if the length of the dateString is equal to 4, then it means that is a year.
- Store that year in an unordered_set.
- Return the size of the unordered_set as it contains only unique values.
Below is the implementation of above approach:
- Find the number of strings formed using distinct characters of a given string
- Find distinct characters in distinct substrings of a string
- Find all distinct palindromic sub-strings of a given string
- Number of distinct permutation a String can have
- Total distinct pairs of ugly numbers from two arrays
- String Range Queries to find the number of subsets equal to a given String
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Distinct permutations of the string | Set 2
- Find the maximum possible Binary Number from given string
- Find the missing number in a string of numbers with no separator
- Find minimum number of steps to reach the end of String
- Minimum changes to a string to make all substrings distinct
- Convert given string so that it holds only distinct characters
- Check if given string can be split into four distinct strings
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.