일부 소문자 알파벳 문자와 하나의 특수 문자 도트(.)가 포함된 문자열이 제공됩니다. 가능한 많은 대체가 가능한 경우 결과 문자열이 회문이 되도록 모든 점을 일부 알파벳 문자로 바꿔야 하며 사전순으로 가장 작은 회문 문자열을 선택해야 합니다. 가능한 모든 교체 후에 문자열을 회문으로 변환하는 것이 불가능하면 Not available을 출력합니다.
예:
Input : str = ab..e.c.a Output : abcaeacba The smallest palindrome which can be made after replacement is 'abcaeacba' We replaced first dot with 'c' second dot with 'a' third dot with 'a' and fourth dot with 'b' Input : str = ab..e.c.b Output : Not Possible It is not possible to convert above string into palindrome
우리는 이 문제를 다음과 같이 해결할 수 있습니다. 결과 문자열은 회문이어야 하기 때문에 시작 시 점이 아닌 문자 쌍을 확인할 수 있습니다. 일치하지 않으면 직접 반환이 불가능합니다. 새 문자를 점 위치에만 배치할 수 있기 때문입니다.
그런 다음 현재 문자가 도트이면 문자열의 문자를 반복하고 해당 문자가 도트이면 쌍을 이루는 문자((n – i -1)번째 위치의 문자)를 확인한 다음 두 문자를 모두 'a'로 바꿀 수 있습니다. 'a'는 가장 작은 소문자 알파벳이므로 끝에 있는 가장 작은 사전식 문자열을 다른 문자로 바꾸면 사전식으로 더 큰 회문식 문자열이 생성됩니다. 다른 경우에 쌍을 이루는 문자가 점이 아닌 경우 문자열 회문을 만들려면 현재 문자를 쌍을 이루는 문자로 바꿔야 합니다.
So in short If both 'i' and 'n- i- 1' are dot replace them by ‘a’ If one of them is a dot character replace that by other non-dot character
위의 절차는 사전순으로 가장 작은 회문 문자열을 제공합니다.
구현:
C++// C++ program to get lexicographically smallest // palindrome string #include using namespace std; // Utility method to check str is possible palindrome // after ignoring . bool isPossiblePalindrome(string str) { int n = str.length(); for (int i=0; i<n/2; i++) { /* If both left and right character are not dot and they are not equal also then it is not possible to make this string a palindrome */ if (str[i] != '.' && str[n-i-1] != '.' && str[i] != str[n-i-1]) return false; } return true; } // Returns lexicographically smallest palindrom // string if possible string smallestPalindrome(string str) { if (!isPossiblePalindrome(str)) return 'Not Possible'; int n = str.length(); // loop through character of string for (int i = 0; i < n; i++) { if (str[i] == '.') { // if one of character is dot replace dot // with other character if (str[n - i - 1] != '.') str[i] = str[n - i - 1]; // if both character are dot then replace // them with smallest character 'a' else str[i] = str[n - i - 1] = 'a'; } } // return the result return str; } // Driver code to test above methods int main() { string str = 'ab..e.c.a'; cout << smallestPalindrome(str) << endl; return 0; }
Java // Java program to get lexicographically // smallest palindrome string class GFG { // Utility method to check str is // possible palindrome after ignoring static boolean isPossiblePalindrome(char str[]) { int n = str.length; for (int i = 0; i < n / 2; i++) { /* If both left and right character are not dot and they are not equal also then it is not possible to make this string a palindrome */ if (str[i] != '.' && str[n - i - 1] != '.' && str[i] != str[n - i - 1]) return false; } return true; } // Returns lexicographically smallest // palindrome string if possible static void smallestPalindrome(char str[]) { if (!isPossiblePalindrome(str)) System.out.println('Not Possible'); int n = str.length; // loop through character of string for (int i = 0; i < n; i++) { if (str[i] == '.') { // if one of character is dot // replace dot with other character if (str[n - i - 1] != '.') str[i] = str[n - i - 1]; // if both character are dot // then replace them with // smallest character 'a' else str[i] = str[n - i - 1] = 'a'; } } // return the result for(int i = 0; i < n; i++) System.out.print(str[i] + ''); } // Driver code public static void main(String[] args) { String str = 'ab..e.c.a'; char[] s = str.toCharArray(); smallestPalindrome(s); } } // This code is contributed // by ChitraNayal
Python 3 # Python 3 program to get lexicographically # smallest palindrome string # Utility method to check str is # possible palindrome after ignoring def isPossiblePalindrome(str): n = len(str) for i in range(n // 2): # If both left and right character # are not dot and they are not # equal also then it is not possible # to make this string a palindrome if (str[i] != '.' and str[n - i - 1] != '.' and str[i] != str[n - i - 1]): return False return True # Returns lexicographically smallest # palindrome string if possible def smallestPalindrome(str): if (not isPossiblePalindrome(str)): return 'Not Possible' n = len(str) str = list(str) # loop through character of string for i in range(n): if (str[i] == '.'): # if one of character is dot # replace dot with other character if (str[n - i - 1] != '.'): str[i] = str[n - i - 1] # if both character are dot # then replace them with # smallest character 'a' else: str[i] = str[n - i - 1] = 'a' # return the result return str # Driver code if __name__ == '__main__': str = 'ab..e.c.a' print(''.join(smallestPalindrome(str))) # This code is contributed by ChitraNayal
C# // C# program to get lexicographically // smallest palindrome string using System; public class GFG { // Utility method to check str is // possible palindrome after ignoring static bool isPossiblePalindrome(char []str) { int n = str.Length; for (int i = 0; i < n / 2; i++) { /* If both left and right character are not dot and they are not equal also then it is not possible to make this string a palindrome */ if (str[i] != '.' && str[n - i - 1] != '.' && str[i] != str[n - i - 1]) return false; } return true; } // Returns lexicographically smallest // palindrome string if possible static void smallestPalindrome(char []str) { if (!isPossiblePalindrome(str)) Console.WriteLine('Not Possible'); int n = str.Length; // loop through character of string for (int i = 0; i < n; i++) { if (str[i] == '.') { // if one of character is dot // replace dot with other character if (str[n - i - 1] != '.') str[i] = str[n - i - 1]; // if both character are dot // then replace them with // smallest character 'a' else str[i] = str[n - i - 1] = 'a'; } } // return the result for(int i = 0; i < n; i++) Console.Write(str[i] + ''); } // Driver code public static void Main() { String str = 'ab..e.c.a'; char[] s = str.ToCharArray(); smallestPalindrome(s); } } // This code is contributed by PrinciRaj1992
PHP // PHP program to get lexicographically // smallest palindrome string // Utility method to check str is // possible palindrome after ignoring function isPossiblePalindrome($str) { $n = strlen($str); for ($i = 0; $i < $n / 2; $i++) { /* If both left and right character are not dot and they are not equal also then it is not possible to make this string a palindrome */ if ($str[$i] != '.' && $str[$n - $i - 1] != '.' && $str[$i] != $str[$n - $i - 1]) return false; } return true; } // Returns lexicographically smallest // palindrome string if possible function smallestPalindrome($str) { if (!isPossiblePalindrome($str)) return 'Not Possible'; $n = strlen($str); // loop through character of string for ($i= 0; $i < $n; $i++) { if ($str[$i] == '.') { // if one of character is dot // replace dot with other character if ($str[$n - $i - 1] != '.') $str[$i] = $str[$n - $i - 1]; // if both character are dot // then replace them with // smallest character 'a' else $str[$i] = $str[$n - $i - 1] = 'a'; } } // return the result return $str; } // Driver code $str = 'ab..e.c.a'; echo smallestPalindrome($str); // This code is contributed // by ChitraNayal ?> JavaScript <script> // Javascript program to get lexicographically // smallest palindrome string // Utility method to check str is // possible palindrome after ignoring function isPossiblePalindrome(str) { let n = str.length; for (let i = 0; i < Math.floor(n / 2); i++) { /* If both left and right character are not dot and they are not equal also then it is not possible to make this string a palindrome */ if (str[i] != '.' && str[n - i - 1] != '.' && str[i] != str[n - i - 1]) return false; } return true; } // Returns lexicographically smallest // palindrome string if possible function smallestPalindrome(str) { if (!isPossiblePalindrome(str)) document.write('Not Possible'); let n = str.length; // loop through character of string for (let i = 0; i < n; i++) { if (str[i] == '.') { // if one of character is dot // replace dot with other character if (str[n - i - 1] != '.') str[i] = str[n - i - 1]; // if both character are dot // then replace them with // smallest character 'a' else str[i] = str[n - i - 1] = 'a'; } } // return the result for(let i = 0; i < n; i++) document.write(str[i] + ''); } // Driver code let str='ab..e.c.a'; let s = str.split(''); smallestPalindrome(s); // This code is contributed by rag2127 </script>
산출
abcaeacba
시간 복잡도: O(n) 여기서 n은 문자열의 길이입니다.
보조 공간 복잡도: O(1)