检查是否可以通过增加前缀使两个字符串相等
在这个问题中,我们必须检查。是否可以通过增加第一个字符串前缀的字符的 ASCII 值使两个字符串相等。我们可以多次增加不同的前缀。
字符串仅由小写字母组成,不包含任何特殊字符。
例子:
Input : string 1=”abcd”, string 2=”bcdd”
Output : Yes
Explanation : The string 1 can be converted to string 2 by increasing the prefix of string_1 by 1 i.e. increasing the ASCII value of the prefix string of “abcd”, “abc” by 1 .
In this way we can convert “abcd” to “bcdd”
Input :string 1=”abcd”, string 2=”bcdg”
Output :No
解决方案只有当第一个字符串的所有 ASCII 值都小于或等于第二字符串字符串字符串的字符值的差为按降序排列。
因此,我们将检查两个字符串是否相等,如果它们相等,那么我们将检查第一个字符串的所有 ASCII 值是否小于或等于第二个字符串,我们将创建一个差异数组来存储两个字符串的字符差异,并检查差异数组是否按降序排列,如果所有条件都满足,则打印“是”,否则打印“否”。
C++
// C++ implementation of above approach
#include
using namespace std;
// check whether the first string can be
// converted to the second string
// by increasing the ASCII value of prefix
// string of first string
bool find(string s1, string s2)
{
// length of two strings
int len = s1.length(), len_1 = s2.length();
// If lengths are not equal
if (len != len_1)
return false;
// store the difference of ASCII values
int d[len] = { 0 };
// difference of first element
d[0] = s2[0] - s1[0];
// traverse through the string
for (int i = 1; i < len; i++) {
// the ASCII value of the second string
// should be greater than or equal to first
// string, if it is violated return false.
if (s1[i] > s2[i])
return false;
else {
// store the difference of ASCII values
d[i] = s2[i] - s1[i];
}
}
// the difference of ASCII values should be
// in descending order
for (int i = 0; i < len - 1; i++) {
// if the difference array is not in descending order
if (d[i] < d[i + 1])
return false;
}
// if all the ASCII values of characters of first string
// is less than or equal to the second string
// and the difference array is in descending
// order, return true
return true;
}
// Driver code
int main()
{
// create two strings
string s1 = "abcd", s2 = "bcdd";
// check whether the first string can be
// converted to the second string
if (find(s1, s2))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java implementation of above approach
class GFG {
// check whether the first string can be
// converted to the second string
// by increasing the ASCII value of prefix
// string of first string
static boolean find(String s1, String s2)
{
// length of two strings
int len = s1.length(), len_1 = s2.length();
// If lengths are not equal
if (len != len_1)
{
return false;
}
// store the difference of ASCII values
int d[] = new int[len];
// difference of first element
d[0] = s2.charAt(0) - s1.charAt(0);
// traverse through the string
for (int i = 1; i < len; i++)
{
// the ASCII value of the second string
// should be greater than or equal to first
// string, if it is violated return false.
if (s1.charAt(i) > s2.charAt(i))
{
return false;
}
else
{
// store the difference of ASCII values
d[i] = s2.charAt(i) - s1.charAt(i);
}
}
// the difference of ASCII values should be
// in descending order
for (int i = 0; i < len - 1; i++)
{
// if the difference array is not in descending order
if (d[i] < d[i + 1])
{
return false;
}
}
// if all the ASCII values of characters
// of first string is less than or equal to
// the second string and the difference array
// is in descending order, return true
return true;
}
// Driver code
public static void main(String[] args)
{
// create two strings
String s1 = "abcd", s2 = "bcdd";
// check whether the first string can be
// converted to the second string
if (find(s1, s2))
{
System.out.println("Yes");
} else
{
System.out.println("No");
}
}
}
// This code is contributed by PrinciRaj1992
C#
// C# implementation of above approach
using System;
class GFG
{
// check whether the first string can be
// converted to the second string
// by increasing the ASCII value of prefix
// string of first string
public static bool find(string s1, string s2)
{
// length of two strings
int len = s1.Length, len_1 = s2.Length;
// If lengths are not equal
if (len != len_1)
return false;
// store the difference of ASCII values
int []d = new int [len];
// difference of first element
d[0] = s2[0] - s1[0];
// traverse through the string
for (int i = 1; i < len; i++)
{
// the ASCII value of the second string
// should be greater than or equal to first
// string, if it is violated return false.
if (s1[i] > s2[i])
return false;
else
{
// store the difference of ASCII values
d[i] = s2[i] - s1[i];
}
}
// the difference of ASCII values should be
// in descending order
for (int i = 0; i < len - 1; i++)
{
// if the difference array is not in descending order
if (d[i] < d[i + 1])
return false;
}
// if all the ASCII values of characters of first string
// is less than or equal to the second string
// and the difference array is in descending
// order, return true
return true;
}
// Driver code
public static void Main()
{
// create two strings
string s1 = "abcd", s2 = "bcdd";
// check whether the first string can be
// converted to the second string
if (find(s1, s2))
Console.WriteLine("Yes");
else
Console.WriteLine( "No");
}
}
// This code is contributed by SoM15242
Python3
# Python 3 implementation of above approach
# check whether the first string can be
# converted to the second string
# by increasing the ASCII value of prefix
# string of first string
def find(s1, s2):
# length of two strings
len__ = len(s1)
len_1 = len(s2)
# If lengths are not equal
if (len__ != len_1):
return False
# store the difference of ASCII values
d = [0 for i in range(len__)]
# difference of first element
d[0] = ord(s2[0]) - ord(s1[0])
# traverse through the string
for i in range(1, len__, 1):
# the ASCII value of the second string
# should be greater than or equal to first
# string, if it is violated return false.
if (s1[i] > s2[i]):
return False
else:
# store the difference of ASCII values
d[i] = ord(s2[i]) - ord(s1[i])
# the difference of ASCII values
# should be in descending order
for i in range(len__ - 1):
# if the difference array is not
# in descending order
if (d[i] < d[i + 1]):
return False
# if all the ASCII values of characters of
# first string is less than or equal to the
# second string and the difference array is
# in descending order, return true
return True
# Driver code
if __name__ == '__main__':
# create two strings
s1 = "abcd"
s2 = "bcdd"
# check whether the first string can
# be converted to the second string
if (find(s1, s2)):
print("Yes")
else:
print("No")
# This code is contributed by
# Shashank_Sharma
Javascript
输出:
Yes
时间复杂度: O(N),其中 N 是 s1字符串的长度
辅助空间: O(N),其中 N 是 s1字符串的长度