Brian built his own car and was confused about what name he should keep for it. He asked Roman for help. Roman being his good friend, suggested a lot of names.
Brian only liked names that:
- Consisted of exactly three distinct characters, say C1, C2, and C3
- Satisfied the criteria that the string was of the form - C1n C2n C3n : This means, first C1 occurs n times, then C2 occurs n times and then C3 occurs n times. For example, xyz, ccaarr, mmmiiiaaa satisfy the criteria, but xyzw, aabbbcccc don't.
Given N names suggested by Roman, print "OK" if Brian likes the name and "Not OK" if he doesn't.
Example:
Input: s = "bbbrrriii"
Output: OK
Approach
C++
#include <bits/stdc++.h>using namespace std;void carNames(string s){int n = s.size();set<char> st;for (int i = 0; i < n; i++)st.insert(s[i]);int len = st.size();if (len == 3){int i = 0;int cnt1 = 1, cnt2 = 1, cnt3 = 1;while (i < n - 1 && s[i] == s[i + 1]){cnt1++;i++;}i++;while (i < n - 1 && s[i] == s[i + 1]){cnt2++;i++;}i++;while (i < n - 1 && s[i] == s[i + 1]){cnt3++;i++;}if ((cnt1 == cnt2) && (cnt2 == cnt3) &&(cnt1 + cnt2 + cnt3) == n)cout << "OK\n";elsecout << "Not OK\n";}elsecout << "Not OK\n";}int main(){string s = "bbbrrriii";carNames(s);return 0;}
No comments:
Post a Comment