-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathChooseAndSwap.txt
58 lines (55 loc) · 1.21 KB
/
ChooseAndSwap.txt
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
class Solution{
public:
string chooseandswap(string a){
// Code Here
string s=a;
char ch='\0',c='\0';
int flag=0;
vector<int> vec(26);
vec={0};
priority_queue<char,vector<char>,greater<char>> pq;
for(int i=0;i<a.length()-1;i++)
{
flag=0;
ch=a[i];
int j=i+1;
vec[a[i]-'a']=-1;
while(j<a.length())
{
if(a[j]<ch && vec[a[j]-'a']!=-1)
{
flag=1;
vec[a[j]-'a']=-1;
pq.push(a[j]);
}
j++;
}
if(!pq.empty())
c=pq.top();
while(!pq.empty())
pq.pop();
if(flag==1)
break;
}
if(c=='\0')
return a;
//cout<<ch<<"****"<<c;
for(int i=0;i<s.length();i++)
{
if(s[i]==ch)
{
s[i]=c;
}
else
if(s[i]==c)
{
s[i]=ch;
}
}
if(a.compare(s)>=0)
{
return s;
}
return a;
}
};