转换一个字符串,使其具有 abcd..z 作为子序列
给定一个只有小英文字母的字符串S。我们需要通过一些移动(任意次数)来转换字符串,以获得字符串“abcdefghijklmnopqrstuvwxyz”作为该字符串中的子序列。在一个步骤中,您可以按字母顺序将字符串中的任何字符替换为下一个字符,即“a”可以替换为“b”,“b”可以替换为“c”等等。字母 'z' 不能被任何字符替换。如果无法从该字符串中获取子序列,则打印“不可能”。
注意:字符串的子序列是在某些位置删除某些字符得到的字符串。
例子:
Input : aaaaaaaaaaaaaaaaaaaaaaaaaa
Output : abcdefghijklmnopqrstuvwxyz
Explanation: Second occurrence of letter 'a' will be
replaced by 'b', third occurrence of letter 'a' will
be first replaced by 'b' and then by 'c' and so on.
Input : helloworld
Output : Not Possible
这个问题可以使用贪心方法来解决。这个想法是首先观察到,在一次移动中,我们只能将任何字符增加到下一个字符。也就是说,'c' 可以递增到 'd'、'e'、'f' 或任何大于 'c' 的字符。因此,我们将创建一个变量ch ,最初初始化为 'a' 并通过迭代字符串,如果我们发现字符串的当前字符不大于 ch,我们将用 ch 替换它并按字母顺序增加 ch 的值.由于 ch 最初等于 'a',我们将找到 'a',将其替换为 ch,增加 ch 以存储 'b' 并在字符串中向前移动,然后如果我们发现任何不大于 'b' 的字符,请替换它与 'b' 并再次递增 ch 以存储 'c' 并在字符串中向前移动。我们将重复这些步骤,直到 ch 达到 'z' 或处理整个字符串。如果在遍历整个字符串之后,ch 没有达到 'z' 则不可能获得所需的子序列。
下面是上述方法的实现。
C++
// C Plus Plus Program to transform the
// given string to contain the required
// subsequence
#include
using namespace std;
// function to transform string with string
// passed as reference
bool transformString(string& s)
{
// initializing the variable ch to 'a'
char ch = 'a';
// if the length of string is less than
// 26, we can't obtain the required
// subsequence
if (s.size() < 26)
return false;
for (int i = 0; i < s.size(); i++) {
// if ch has reached 'z', it means we have
// transformed our string such that required
// subsequence can be obtained
if (int(ch) > int('z'))
break;
// current character is not greater than ch,
// then replace it with ch and increment ch
if (s[i] <= ch) {
s[i] = ch;
ch = char(int(ch) + 1);
}
}
if (ch <= 'z')
return false;
return true;
}
// Driver Code
int main()
{
string str = "aaaaaaaaaaaaaaaaaaaaaaaaaa";
if (transformString(str))
cout << str << endl;
else
cout << "Not Possible" << endl;
return 0;
}
Java
// Java Program to transform the given string
// to contain the required subsequence
import java.io.*;
public class GFG {
// function to transform string with
// string passed as reference
static boolean transformString(StringBuilder s)
{
// initializing the variable ch to 'a'
char ch = 'a';
// if the length of string is less than
// 26, we can't obtain the required
// subsequence
if (s.length() < 26)
return false;
for (int i = 0; i < s.length(); i++) {
// if ch has reached 'z', it means
// we have transformed our string
// such that required subsequence
// can be obtained
if ((int)ch > (int)'z')
break;
// current character is not greater
// than ch, then replace it with
// ch and increment ch
if (s.charAt(i) <= ch) {
s.setCharAt(i, ch);
ch = (char)((int)ch + 1);
}
}
if (ch <= 'z')
return false;
return true;
}
// Driver Code
public static void main(String args[])
{
StringBuilder str =
new StringBuilder("aaaaaaaaaaaaaaaaaaaaaaaaaa");
if (transformString(str))
System.out.println(str.toString());
else
System.out.println("Not Possible");
}
}
// This code is contributed by Manish Shaw
// (manishshaw1)
Python3
# Python3 Program to transform the
# given string to contain the required
# subsequence
# function to transform string with string
# passed as reference
def transformString(s) :
# initializing the variable ch to 'a'
ch = 'a'
# if the length of string is less than
# 26, we can't obtain the required
# subsequence
if (len(s) < 26) :
return False
for i in range(0, len(s)):
# if ch has reached 'z', it means we have
# transformed our string such that required
# subsequence can be obtained
if (ord(ch) > ord('z')) :
break
# current character is not greater than ch,
# then replace it with ch and increment ch
if (s[i] <= ch) :
s[i] = ch
ch = chr(ord(ch) + 1)
if (ch <= 'z') :
print ("Not Possible")
print ("".join(s))
# Driver Code
s = list("aaaaaaaaaaaaaaaaaaaaaaaaaa")
transformString(s)
# This code is contributed by Manish Shaw
# (manishshaw1)
C#
// C# Program to transform the given string
// to contain the required subsequence
using System;
using System.Text;
using System.Collections.Generic;
class GFG {
// function to transform string with string
// passed as reference
static bool transformString(ref StringBuilder s)
{
// initializing the variable ch to 'a'
char ch = 'a';
// if the length of string is less than
// 26, we can't obtain the required
// subsequence
if (s.Length < 26)
return false;
for (int i = 0; i < s.Length; i++) {
// if ch has reached 'z', it means we
// have transformed our string such
// that required subsequence can be
// obtained
if ((int)ch > 122)
break;
// current character is not greater
// than ch, then replace it with ch
// and increment ch
if (s[i] <= ch) {
s[i] = ch;
ch = (char)((int)ch + 1);
}
}
if (ch <= 'z')
return false;
return true;
}
// Driver Code
public static void Main()
{
StringBuilder str = new
StringBuilder("aaaaaaaaaaaaaaaaaaaaaaaaaa");
if (transformString(ref str))
Console.WriteLine(str + "\n");
else
Console.WriteLine("Not Possible" + "\n");
}
}
// This code is contributed by Manish Shaw
// (manishshaw1)
PHP
ord("z"))
break;
// current character is not
// greater than ch, then
// replace it with ch and
// increment ch
if ($s[$i] <= $ch)
{
$s[$i] = $ch;
$ch = chr(ord($ch) + 1);
}
}
if ($ch <= "z")
return false;
return true;
}
// Driver Code
$str = "aaaaaaaaaaaaaaaaaaaaaaaaaa";
if (transformString($str))
echo $str;
else
echo "Not Possible";
// This code is contributed by
// Manish Shaw (manishshaw1)
?>
Javascript
输出:
abcdefghijklmnopqrstuvwxyz
时间复杂度:O(n),其中 n 是字符串的长度。