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 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
- 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
- Check if given string can be split into four distinct strings
- Convert given string so that it holds only distinct characters
- Minimum changes to a string to make all substrings distinct
- Print all distinct characters of a string in order (3 Methods)
- Distinct state codes that appear in a string as contiguous sub-strings
- Check whether count of distinct characters in a string is Prime or not
- Distinct palindromic sub-strings of the given string using Dynamic Programming
- Count of distinct substrings of a string using Suffix Trie
- Java Program to print distinct permutations of a string
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.