-
Notifications
You must be signed in to change notification settings - Fork 0
/
BeautifulString.cpp
87 lines (62 loc) · 1.91 KB
/
BeautifulString.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//////////////////////////////////////CODE STUDIO////////////////////////////////////
// EASY
/*
a beautiful string is a binary string consisting of only 0s and 1s
eg: "10101" , "01010","010",etc
a beautiful string starts and ends with same element
and also follows a alternative pattern
In this question we have to find the beatiful string for a given binary string
with minimum number of changes
eg: "1001010" this can be converted to : "1010101" ---> 5 changes
: "0101010" ---> 2 changes
we have to return the string with minimum changes made
*/
/*
1. Generate function will generate beautiful string
takes the length of the given string and a boolean zero var
zero describes whether or not there's a zero at the start of the string
if zero is present result string will take zeroes
otherwise it will take ones
a character var temp will be assigned 0/1
on the condition that the last element of result string is 0
if it is, then temp =1 , otherwise 0
and result += temp
this will generate 2 possible beautiful strings
string generate(int n, bool zero){
string res="";
if(n==0) return res;
if(zero){
res+='0';
n--;
}else{
res+='1';
n--;
}
while(n!=0){
char temp=(res[res.size()-1]=='0') ? '1' : '0';
res+=temp;
n--;
}
return res;
}
2. This function will compare the given binary string with geneartaed beautiful string
and set a counter for no. of changes made
int change(string str, string s, int n){
int ans=0;
for(int i=0;i<n;i++){
if(str[i]!=s[i]){
ans++;
}
}
return ans;
}
int makeBeautiful(string str) {
// Write your code here
int n=str.size();
string s1 = generate(n,true);
int dif1=change(str,s1,n);
string s2 = generate(n,false);
int dif2=change(str,s2,n);
return min(dif1,dif2);
}
*/