In: Computer Science
Create a function that keeps only strings with repeating identical characters (in other words, it has a set size of 1).
Examples:
identicalFilter(["aaaaaa", "bc", "d", "eeee", "xyz"]) ➞ ["aaaaaa", "d", "eeee"]
identicalFilter(["88", "999", "22", "545", "133"]) ➞ ["88", "999", "22"]
identicalFilter(["xxxxo", "oxo", "xox", "ooxxoo", "oxo"]) ➞ []
Notes A string with a single character is trivially counted as a string with repeating identical characters. If there are no strings with repeating identical characters, return an empty array.
Code in C++ language ?
Code:
#include <bits/stdc++.h>
using namespace std;
vector<string> identicalFilter(vector<string> input){
vector<string> ans;
for(auto c:input)
{
set<char> s(c.begin(), c.end());
if(s.size()==1)
ans.push_back(c);
}
return ans;
}
void print(vector<string> v){
cout<<"[";
for(int i=0;i<v.size();i++)
cout<<v[i]<<", ";
cout<<"]";
cout<<endl;
}
int main() {
vector<string> ans = identicalFilter(vector<string>{"aaaaaa", "bc", "d", "eeee", "xyz"});
print(ans);
ans = identicalFilter(vector<string>{"88", "999", "22", "545", "133"});
print(ans);
ans = identicalFilter(vector<string>{"xxxxo", "oxo", "xox", "ooxxoo", "oxo"});
print(ans);
return 0;
}
Please comment in case of doubts or queries.
Kindly upvote :)