이 튜토리얼에서는 고유한 문자만 포함하도록 주어진 문자열을 변환하는 프로그램에 대해 논의할 것입니다.
이를 위해 문자열이 제공됩니다. 우리의 임무는 문자열을 순회하고 모든 반복 문자를 문자열에 없는 임의의 문자로 바꾸는 것입니다.
예시
#include<bits/stdc++.h>
using namespace std;
//collecting the distinct characters
//in the string
int calculate_zero(int i, int occurrences[]){
while (i < 26) {
//if it is present only once
if (occurrences[i] == 0)
return i;
i++;
}
//if all are doubles or more
return -1;
}
//printing the modified string
string print_modified(string str) {
int n = str.length();
//if conversion
//not possible
if (n > 26)
return "-1";
string ch = str;
int i, occurrences[26] = {0};
//counting the occurrences
for (i = 0; i < n; i++)
occurrences[ch[i] - 'a']++;
int index = calculate_zero(0, occurrences);
for (i = 0; i < n; i++) {
//replacing the character
if (occurrences[ch[i] - 'a'] > 1) {
occurrences[ch[i] - 'a']--;
ch[i] = (char)('a' + index);
occurrences[index] = 1;
//moving to the next character
index = calculate_zero(index + 1, occurrences);
}
}
cout << ch << endl;
}
int main() {
string str = "tutorialspoint";
print_modified(str);
} 출력
bucdrealspoint