Between the Offices
Hazrat Ali
You prefer flying from Seattle to San Francisco than in the other direction, because it's warmer in San Francisco. You are so busy that you don't remember the number of flights you have made in either direction. However, for each of the last n days you know whether you were in San Francisco office or in Seattle office. You always fly at nights, so you never were at both offices on the same day. Given this information, determine if you flew more times from Seattle to San Francisco during the last n days, or not.
The first line of input contains single integer n (2 ≤ n ≤ 100) — the number of days.
The second line contains a string of length n consisting of only capital 'S' and 'F' letters. If the i-th letter is 'S', then you were in Seattle office on that day. Otherwise you were in San Francisco. The days are given in chronological order, i.e. today is the last day in this sequence.
Print "YES" if you flew more times from Seattle to San Francisco, and "NO" otherwise.
You can print each letter in any case (upper or lower).
4
FSSF
NO
2
SF
YES
10
FFFFFFFFFF
NO
10
SSFFSFFSFF
YES
Solution
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
string s;
cin >> s;
int office = 0, temp = 0;
for (int i = 1; i < s.size(); i++)
{
if (s[i - 1] != s[i])
{
if (s[i - 1] == 'S')
{
office++;
}
else
{
temp++;
}
}
}
if (office > temp)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
return 0;
}