Chandu is very fond of strings. (Or so he thinks!) But, he does not like strings which have same consecutive letters. No one has any idea why it is so. He calls these strings as Bad strings. So, Good strings are the strings which do not have same consecutive letters. Now, the problem is quite simple. Given a string S, you need to convert it into a Good String.
You simply need to perform one operation - if there are two same consecutive letters, delete one of them.
Example:
Input: s = "abb"
Output: ab
Approach
C++
#include <bits/stdc++.h>using namespace std;string consecutiveLetters(string s){int n = s.size();int i = 0;string str = "";while (i < n){while (i < n - 1 && s[i] == s[i + 1])i++;str += s[i];i++;}return str;}int main(){string s = "abb";cout << consecutiveLetters(s) << "\n";return 0;}
No comments:
Post a Comment