-
Notifications
You must be signed in to change notification settings - Fork 0
/
CorruptedMessage.cpp
executable file
·54 lines (50 loc) · 1.07 KB
/
CorruptedMessage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream>
#include<string>
using namespace std;
class CorruptedMessage
{
public:
string reconstructMessage(string strMsg, int n)
{
if( n == 0) return strMsg;
int l = strMsg.length();
for(int i = 0; i < l; ++i)
{
int count = 0;
for(int j = 0; j < l; ++j)
{
if(strMsg[i] != strMsg[j])
count++;
}
if(count == n)
return string(strMsg.size(),strMsg[i]);
}
if( l == n)
{
char ch = 'a';
for(int i = 97; i < 123;++i)
{
ch = i;
if(strMsg.find(ch) == string::npos)
{
break;
}
}
return string(strMsg.size(),ch);
}
//ideally should not come here, as per input constraint
//return null
return strMsg;
}
};
int main()
{
CorruptedMessage CM;
cout<<CM.reconstructMessage("hello",3)<<endl;
cout<<CM.reconstructMessage("abc",3)<<endl;
cout<<CM.reconstructMessage("wwwwwwwwwwwwwwwwww",0)<<endl;
cout<<CM.reconstructMessage("ababba",3)<<endl;
cout<<CM.reconstructMessage("ozztxtoxytyt",10)<<endl;
cout<<CM.reconstructMessage("jlmnmiunaxzywed",13)<<endl;
return 0;
}