Computer Science, asked by chinmaypathak9680, 7 months ago

RevisitQuestion #4How to attempt?QuestionDocumentsThe United Nations Organization released an official document regarding themost important events from the beginning of time (dated 00-00-0000) with a briefdescription of the events. The date of all the events is mentioned in the 'DD-MM-YYYY formatFind the total number of distinct years referenced in the document.Input Specification:input1: String containing the content of the documentOutput Specification:Return the total number of distinct years referenced in the document.mettiSwetha | Support +1-650-9 write the answer in java programming​

Answers

Answered by sudharmani758
15

Answer:

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.

Examples:

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 .

Approach:

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:

// C++ Program to find the total

// number of distinct years

#include <bits/stdc++.h>

using namespace std;

// function to find the total

// number of distinct years

int distinct_year(string str)

{

string str2 = "";

unordered_set<string> uniqueDates;

for (int i = 0; i < str.length(); i++) {

if (isdigit(str[i])) {

str2.push_back(str[i]);

}

// if we found - then clear the str2

if (str[i] == '-') {

str2.clear();

}

// if length of str2 becomes 4

// then store it in a set

if (str2.length() == 4) {

uniqueDates.insert(str2);

str2.clear();

}

}

// return the size of set

return uniqueDates.size();

}

// Driver code

int main()

{

string str = "UN was established on 24-10-1945."

"India got freedom on 15-08-1947.";

cout << distinct_year(str);

return 0;

}

Time Complexity:O(n)

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.

Explanation:

I hope you understand

Answered by mindfulmaisel
25

total number of distinct years

Explanation:

import re

# string input containing content of document

text = input()

matches = re.findall(r'(\d+-\d+-\d+)',text)

# extracting distinct years

years = set(i[-4:] for i in matches)

print("Total no. of distinct years = {}\nDistinct Years: {}".format(len(years), years))

Similar questions