查找在相应索引处具有不同字符的给定字符串的字谜
给定一个由N个字符组成的字符串S ,任务是找到给定字符串S的字谜,使得相同索引处的字符与原始字符串不同。
例子:
Input: S = “geek”
Output: egke
Explanation:
The anagram of the given string such that all the characters at all the corresponding indices are not same is “egke”.
Input: S = “aaaa”
Output: -1
方法:给定的问题可以通过使用两个指针的方法来解决。这个想法是交换字符串末尾的不同字符,如果这些索引处的字符相同,则检查下一个可能具有不同字符的索引对。执行上述操作后,检查 anagram 是否在每个索引处生成不同的字符并相应地打印结果。请按照以下步骤解决给定的问题:
- 初始化一个字符串,比如存储给定字符串S的T。
- 初始化两个指针,比如i和j分别为0和(N – 1) 。
- 迭代直到i的值小于N并且j为非负数并执行以下步骤:
- 如果字符S[i]和S[j]不相同并且字符对(S[i], T[j])和(T[i], S[j])也不相同(确保交换操作后的字符与原来的相同),然后交换字符(S[i], S[j])并将j的值更新为(N – 1) 。
- 否则,将i的值增加1 。
- 如果字符串有奇数个字符并且中间索引处的字符相等,则执行上述步骤所示的交换操作。
- 完成上述步骤后,如果字符S和T对应索引处的字符串不相同,则打印字符串S作为结果字符串。否则,打印“-1” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find anagram of string
// such that characters at the same
// indices are different
void findAnagram(string s)
{
// Copying our original string
// for comparison
string check = s;
// Declaring the two pointers
int i = 0, j = s.length() - 1;
while (i < s.length() && j >= 0) {
// Checking the given condition
if (s[i] != s[j] && check[i] != s[j]
&& check[j] != s[i]) {
swap(s[i], s[j]);
i++;
j = s.length() - 1;
}
else {
j--;
}
}
// When string length is odd
if (s.length() % 2 != 0) {
// The mid element
int mid = s.length() / 2;
// If the characters are the
// same, then perform the swap
// operation as illustrated
if (check[mid] == s[mid]) {
for (int i = 0; i < s.length(); i++) {
if (check[i] != s[mid]
&& s[i] != s[mid]) {
swap(s[i], s[mid]);
break;
}
}
}
}
// Check if the corresponding indices
// has the same character or not
bool ok = true;
for (int i = 0; i < s.length(); i++) {
if (check[i] == s[i]) {
ok = false;
break;
}
}
// If string follows required
// condition
if (ok)
cout << s;
else
cout << -1;
}
// Driver Code
int main()
{
string S = "geek";
findAnagram(S);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find anagram of string
// such that characters at the same
// indices are different
static void findAnagram(String s)
{
// Copying our original string
// for comparison
String check = s;
// Declaring the two pointers
int i = 0, j = s.length() - 1;
while (i < s.length() && j >= 0) {
// Checking the given condition
if (s.charAt(i) != s.charAt(j) && check.charAt(i) != s.charAt(j)
&& check.charAt(j) != s.charAt(i)) {
char temp = s.charAt(i);
s = s.substring(0, i) + s.charAt(j) + s.substring(i + 1);
s = s.substring(0, j) + temp + s.substring(j + 1);
i++;
j = s.length() - 1;
}
else {
j--;
}
}
// When string length is odd
if (s.length() % 2 != 0) {
// The mid element
int mid = s.length() / 2;
// If the characters are the
// same, then perform the swap
// operation as illustrated
if (check.charAt(mid) == s.charAt(mid)) {
for (i = 0; i < s.length(); i++) {
if (check.charAt(i) != s.charAt(mid)
&& s.charAt(i) != s.charAt(mid)) {
char temp = s.charAt(i);
s = s.substring(0, i) + s.charAt(mid) + s.substring(i + 1);
s = s.substring(0, mid) + temp + s.substring(mid + 1);
break;
}
}
}
}
// Check if the corresponding indices
// has the same character or not
boolean ok = true;
for (i = 0; i < s.length(); i++) {
if (check.charAt(i) == s.charAt(i) ) {
ok = false;
break;
}
}
// If string follows required
// condition
if (ok)
System.out.println(s);
else
System.out.println(-1);
}
// Driver Code
public static void main (String[] args)
{
String S = "geek";
findAnagram(S);
}
}
// This code is contributed by sanjoy_62.
Python3
# Python 3 program for the above approach
# Function to find anagram of string
# such that characters at the same
# indices are different
def findAnagram(s):
# Copying our original string
# for comparison
check = s
st = list(s)
# Declaring the two pointers
i = 0
j = len(st) - 1
while (i < len(st) and j >= 0):
# Checking the given condition
if (st[i] != st[j] and check[i] != st[j]
and check[j] != st[i]):
st[i], st[j] = st[j], st[i]
i += 1
j = len(st) - 1
else:
j -= 1
# When string length is odd
if (len(st) % 2 != 0):
# The mid element
mid = len(st) / 2
# If the characters are the
# same, then perform the swap
# operation as illustrated
if (check[mid] == st[mid]):
for i in range(len(st)):
if (check[i] != st[mid]
and st[i] != st[mid]):
st[i], st[mid] = st[mid], st[i]
break
# Check if the corresponding indices
# has the same character or not
ok = True
for i in range(len(st)):
if (check[i] == st[i]):
ok = False
break
# If string follows required
# condition
if (ok):
print("".join(st))
else:
print(-1)
# Driver Code
if __name__ == "__main__":
S = "geek"
findAnagram(S)
# This code is contributed by ukasp.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find anagram of string
// such that characters at the same
// indices are different
static void findAnagram(string s)
{
// Copying our original string
// for comparison
string check = s;
// Declaring the two pointers
int i = 0, j = s.Length - 1;
while (i < s.Length && j >= 0) {
// Checking the given condition
if (s[i] != s[j] && check[i] != s[j]
&& check[j] != s[i]) {
char temp = s[i];
s = s.Substring(0, i) + s[j] + s.Substring(i + 1);
s = s.Substring(0, j) + temp + s.Substring(j + 1);
i++;
j = s.Length - 1;
}
else {
j--;
}
}
// When string length is odd
if (s.Length % 2 != 0) {
// The mid element
int mid = s.Length / 2;
// If the characters are the
// same, then perform the swap
// operation as illustrated
if (check[mid] == s[mid]) {
for (i = 0; i < s.Length; i++) {
if (check[i] != s[mid]
&& s[i] != s[mid]) {
char temp = s[i];
s = s.Substring(0, i) + s[mid] + s.Substring(i + 1);
s = s.Substring(0, mid) + temp + s.Substring(mid + 1);
break;
}
}
}
}
// Check if the corresponding indices
// has the same character or not
bool ok = true;
for (i = 0; i < s.Length; i++) {
if (check[i] == s[i]) {
ok = false;
break;
}
}
// If string follows required
// condition
if (ok)
Console.Write(s);
else
Console.Write(-1);
}
// Driver Code
public static void Main()
{
string S = "geek";
findAnagram(S);
}
}
// This code is contributed by ipg2016107.
Javascript
输出:
egke
时间复杂度: O(N*log N)
辅助空间: O(N)