इस ट्यूटोरियल में, हम दिए गए स्ट्रिंग को कन्वर्ट करने के लिए एक प्रोग्राम पर चर्चा करेंगे ताकि इसमें केवल अलग-अलग वर्ण हों।
इसके लिए हमें एक स्ट्रिंग प्रदान की जाएगी। हमारा काम स्ट्रिंग के माध्यम से पार करना है और सभी आवर्ती वर्णों को किसी भी यादृच्छिक वर्णों से बदलना है जो पहले से ही स्ट्रिंग में मौजूद नहीं हैं।
उदाहरण
#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