Check whether two strings can be made equal by increasing prefixes
Last Updated :
07 Sep, 2022
In this problem we have to check. whether two strings can be made equal by increasing the ASCII value of characters of the prefix of first string. We can increase different prefixes multiple times.
The strings consists of only lowercase alphabets and does not contain any special characters .
Examples:
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
Solution: Two strings can be made equal to each other by increasing the ASCII value of the prefix of the first string only when all the ASCII values of the first string is less than or equal to the second string and the difference of ASCII values of characters are in descending order.
So we will check if both the strings are equal or not, if they are equal then we will check that all the ASCII values of first string is less than or equal to the second string or not, and we will create a different array that will store the difference of characters of two strings and check if the difference array is in descending order or not, if all the conditions are satisfied we will print “Yes” else “No”.
Implementation:
C++
#include <iostream>
using namespace std;
bool find(string s1, string s2)
{
int len = s1.length(), len_1 = s2.length();
if (len != len_1)
return false ;
int d[len] = { 0 };
d[0] = s2[0] - s1[0];
for ( int i = 1; i < len; i++) {
if (s1[i] > s2[i])
return false ;
else {
d[i] = s2[i] - s1[i];
}
}
for ( int i = 0; i < len - 1; i++) {
if (d[i] < d[i + 1])
return false ;
}
return true ;
}
int main()
{
string s1 = "abcd" , s2 = "bcdd" ;
if (find(s1, s2))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG {
static boolean find(String s1, String s2)
{
int len = s1.length(), len_1 = s2.length();
if (len != len_1)
{
return false ;
}
int d[] = new int [len];
d[ 0 ] = s2.charAt( 0 ) - s1.charAt( 0 );
for ( int i = 1 ; i < len; i++)
{
if (s1.charAt(i) > s2.charAt(i))
{
return false ;
}
else
{
d[i] = s2.charAt(i) - s1.charAt(i);
}
}
for ( int i = 0 ; i < len - 1 ; i++)
{
if (d[i] < d[i + 1 ])
{
return false ;
}
}
return true ;
}
public static void main(String[] args)
{
String s1 = "abcd" , s2 = "bcdd" ;
if (find(s1, s2))
{
System.out.println( "Yes" );
} else
{
System.out.println( "No" );
}
}
}
|
C#
using System;
class GFG
{
public static bool find( string s1, string s2)
{
int len = s1.Length, len_1 = s2.Length;
if (len != len_1)
return false ;
int []d = new int [len];
d[0] = s2[0] - s1[0];
for ( int i = 1; i < len; i++)
{
if (s1[i] > s2[i])
return false ;
else
{
d[i] = s2[i] - s1[i];
}
}
for ( int i = 0; i < len - 1; i++)
{
if (d[i] < d[i + 1])
return false ;
}
return true ;
}
public static void Main()
{
string s1 = "abcd" , s2 = "bcdd" ;
if (find(s1, s2))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Python3
def find(s1, s2):
len__ = len (s1)
len_1 = len (s2)
if (len__ ! = len_1):
return False
d = [ 0 for i in range (len__)]
d[ 0 ] = ord (s2[ 0 ]) - ord (s1[ 0 ])
for i in range ( 1 , len__, 1 ):
if (s1[i] > s2[i]):
return False
else :
d[i] = ord (s2[i]) - ord (s1[i])
for i in range (len__ - 1 ):
if (d[i] < d[i + 1 ]):
return False
return True
if __name__ = = '__main__' :
s1 = "abcd"
s2 = "bcdd"
if (find(s1, s2)):
print ( "Yes" )
else :
print ( "No" )
|
Javascript
<script>
function find(s1, s2)
{
var len = s1.length, len_1 = s2.length;
if (len != len_1)
return false ;
var d = Array(len).fill(0);
d[0] = s2[0] - s1[0];
for ( var i = 1; i < len; i++) {
if (s1[i] > s2[i])
return false ;
else {
d[i] = s2[i] - s1[i];
}
}
for ( var i = 0; i < len - 1; i++) {
if (d[i] < d[i + 1])
return false ;
}
return true ;
}
var s1 = "abcd" , s2 = "bcdd" ;
if (find(s1, s2))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Complexity analysis:
- Time Complexity: O(N), where N is the length of s1 string
- Auxiliary Space: O(N), where N is the length of s1 string
Share your thoughts in the comments
Please Login to comment...