-
Notifications
You must be signed in to change notification settings - Fork 0
/
choose_and_swap.cpp
53 lines (48 loc) · 1.22 KB
/
choose_and_swap.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
/*
You are given a string s of lower case english alphabets. You can choose any two characters in the string and replace all
the occurences of the first character with the second character and replace all the occurences of the second character with
the first character. Your aim is to find the lexicographically smallest string that can be obtained by doing this operation
at most once.
*/
#include <bits/stdc++.h>
using namespace std;
string chooseAndswap(string s){
//array to store left most occurence of every character
int left_most[26];
for(int i = 0; i < 26; i++){
left_most[i] = -1;
}
for(int i = 0; i < s.length(); i++){
if(left_most[s[i]-'a'] == -1){
left_most[s[i]-'a'] = i;
}
}
int i,j;
//find the first character from left to right which has a smaller
//character on its left
for(i = 0; i < s.length(); i++){
for(j = 0; j < 26; j++){
if(j < (s[i]-'a') && left_most[j] > left_most[s[i]-'a'])
break;
}
if(j != 26) break;
}
//swap all positions of characters
char x = j+'a';
char y = s[i];
for(i = 0; i < s.length(); i++){
if(s[i] == x){
s[i] = y;
}
else if(s[i] == y){
s[i] = x;
}
}
return s;
}
int main(){
string s;
cin >> s;
cout << chooseAndswap(s) << endl;
return 0;
}