777D. Cloud of Hashtags - Codeforces Solution C++

  Problem Link : 777D. Cloud of Hashtags 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;

#define ll long long int

int main()
{
	int n;
	cin>>n;
	string arr[n];
	for(int i=0;i<n;i++)
		cin>>arr[i];

	for(int i=n-2;i>=0;i--)
	{
		int j=0;
		while(j<arr[i].length() && j<arr[i+1].length() && arr[i][j]==arr[i+1][j])
		{
			j++;
		}
		if(j==arr[i].length())
			continue;
		if(j==arr[i+1].length())
		{
			arr[i]=arr[i+1];
			continue;
		}

		if(arr[i][j]>arr[i+1][j])
		{
			arr[i]=arr[i].substr(0,j);
			continue;
		}
	}

	for(auto it : arr)
		cout<<it<<endl;


}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!