Find total number of distinct years from a string
Given a string containing the words and dates, the task is to find the number of distinct years mentioned.
Note: Assuming that the date will be in ‘DD-MM-YYYY’ format and the string will end with a full stop.
Examples:
Input: str = “UN was established on 24-10-1945. India got freedom on 15-08-1947.”
Output: 2
Explanation: 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
Explanation: 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 it is a year.
- Store that year in an unordered_set.
- Return the size of the unordered_set as it contains only unique values.
Note: For Python, ‘(\d+-\d+-\d+)’ can be used in place of ‘[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]’ as an alternate regular expression.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
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]);
}
else if (str[i] == '-' ) {
str2.clear();
}
else if (str2.length() == 4) {
uniqueDates.insert(str2);
str2.clear();
}
else {
str2.clear();
}
}
return uniqueDates.size();
}
int main()
{
string str = "UN was established on 24-10-1945."
"India got freedom on 15-08-1947." ;
cout << distinct_year(str);
return 0;
}
|
Java
import java.util.HashSet;
import java.util.Set;
public class GFG {
static int distinct_year(String str) {
String str2 = "" ;
Set<String> uniqueDates = new HashSet<>();
for ( int i = 0 ; i < str.length(); i++) {
if (Character.isDigit(str.charAt(i))) {
str2 += (str.charAt(i));
}
if (str.charAt(i) == '-' ) {
str2 = "" ;
}
if (str2.length() == 4 ) {
uniqueDates.add(str2);
str2 = "" ;
}
}
return uniqueDates.size();
}
static public void main(String[] args) {
String str = "UN was established on 24-10-1945."
+ "India got freedom on 15-08-1947." ;
System.out.println(distinct_year(str));
}
}
|
Python3
import re
def distinct_years( str ):
matches = re.findall(r '(\d+-\d+-\d+)' , str )
years = set (i[ - 4 :] for i in matches)
return len (years)
if __name__ = = "__main__" :
str = "UN was established on 24 - 10 - 1945. \
India got freedom on 15 - 08 - 1947. "
print (distinct_years( str ))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int distinct_year(String str)
{
String str2 = "" ;
HashSet<String> uniqueDates = new HashSet<String>();
for ( int i = 0; i < str.Length; i++)
{
if ( char .IsDigit(str[i]))
{
str2 += (str[i]);
}
if (str[i] == '-' )
{
str2 = "" ;
}
if (str2.Length == 4)
{
uniqueDates.Add(str2);
str2 = "" ;
}
}
return uniqueDates.Count;
}
static public void Main(String[] args)
{
String str = "UN was established on 24-10-1945." +
"India got freedom on 15-08-1947." ;
Console.WriteLine(distinct_year(str));
}
}
|
Javascript
<script>
function distinct_year(str)
{
var str2 = "" ;
var uniqueDates = new Set();
for ( var i = 0; i < str.length; i++) {
if (parseInt(str[i])) {
str2+=(str[i]);
}
if (str[i] == '-' ) {
str2 = "" ;
}
if (str2.length == 4) {
uniqueDates.add(str2);
str2 = "" ;
}
}
return uniqueDates.size;
}
var str = "UN was established on 24-10-1945." +
"India got freedom on 15-08-1947." ;
document.write( distinct_year(str));
</script>
|
Time Complexity: O(n), where n is the size of the given string.
Auxiliary Space: O(n)
Last Updated :
23 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...