A string s is called a good string if and only if two consecutive letters are not the same. For example, "abcab" and "cda" are good while "abaa" and "accba" are not.
You are given a string s. Among all the good substrings of s, print the size of the longest one.
Example:
Input: s = "ab"
Output: 2
Approach
C++
#include <bits/stdc++.h>using namespace std;int oneStringNoTrouble(string s){int n = s.size();int i = 0;int ans = INT_MIN;while (i < n){int cnt = 1;while (i < n - 1 && s[i] != s[i + 1]){cnt++;i++;}ans = max(ans, cnt);i++;}return ans;}int main(){string s = "ab";cout << oneStringNoTrouble(s) << "\n";return 0;}
No comments:
Post a Comment