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:
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.
- Total distinct pairs of ugly numbers from two arrays
- Count of Distinct Substrings occurring consecutively in a given String
- Program to count number of distinct Squares and Cubes upto N
- Equally divide into two sets such that one set has maximum distinct elements
- Count distinct elements in an array
- Count of pairs between two arrays such that the sums are distinct
- std::string::crbegin() and std::string::crend() in C++ with Examples
- Remove Duplicate/Repeated words from String
- Commonly used String functions in C/C++ with Examples
- std::string::front() in C++with Examples
- std::string::back() in C++ with Examples
- Find if there is a subarray with 0 sum
- Find the only repetitive element between 1 to n-1
- Find the only element that appears b times
- multimap find() in C++ STL
- set find() function in C++ STL
- unordered_multimap find() function in C++ STL
- map find() function in C++ STL
- multiset find() function in C++ STL
- unordered_set find() function in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.