Little robot CJ-17 is exploring ancient ruins. He found a piece of paper with a word written on it. Fortunately, people who used to live at this location several thousand years ago used only two letters of modern English alphabet:
'a'
and 'b'
. It's also known, that no ancient word contains two letters 'a' in a row. CJ-17 has already recognized some of the word letters, the others are still unknown.CJ-17 wants to look up all valid words that could be written on this paper in an ancient dictionary. He needs your help. Find him the word, which is the first in alphabetical order and could be written on the paper.
Example:
Input: s ="?ba??b"
Output: ababab
Approach
C++
#include <bits/stdc++.h>using namespace std;string exploringRuins(string s){int n = s.size();if (s.size() == 1){if (s[0] == '?')s[0] = 'a';return s;}else{for (int i = 0; i < n; i++){if (i == 0){if (s[i] == '?' && s[i + 1] == '?')s[i] = 'a';else if (s[i] == '?' && s[i + 1] == 'b')s[i] = 'a';else if (s[i] == '?' && s[i + 1] == 'a')s[i] = 'b';}else if (i == n - 1){if (s[i] == '?'){if (s[i - 1] == 'a')s[i] = 'b';elses[i] = 'a';}}else{if (s[i] == '?'){if (s[i - 1] == 'a')s[i] = 'b';else if (s[i - 1] == 'b' && s[i + 1] == 'a')s[i] = 'b';elses[i] = 'a';}}}return s;}}int main(){string s = "?ba??b";cout << exploringRuins(s) << "\n";return 0;}
No comments:
Post a Comment