Check whether two strings can be made equal by increasing prefixes
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 the all the ASCII values of 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, we will create a difference 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”.
C++
// C++ implementation of above approach #include <iostream> 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 |
Output:
Yes
Recommended Posts:
- Check if two strings can be made equal by swapping one character among each other
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Check whether the string S1 can be made equal to S2 with the given operation
- Print all possible strings that can be made by placing spaces
- Count number of strings (made of R, G and B) using given combination
- Print all possible strings that can be made by placing spaces
- Find the longest string that can be made up of other strings from the array
- Count of strings that become equal to one of the two strings after one removal
- Check if a given string is made up of two alternating characters
- Check if a two character string can be made using given words
- Number of ways to divide string in sub-strings such to make them in lexicographically increasing sequence
- Check if string can be made lexicographically smaller by reversing any substring
- Meta Strings (Check if two strings can become same after a swap in one string)
- Minimum move to end operations to make all strings equal
- Number of character corrections in the given strings to make them equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


