Given a string of length n and a non-negative integer k. Find k distant string of given string.
Distance between two letters is difference between their positions in the alphabet. for example:
- dist(c, e) = dist(e, c) = 2.
- dist(a, z) = dist(z, a) = 25.
By using this concept, the distance between two strings is the sum of distances of corresponding letters. For example :
- dist(af, hf) = dist(a, h) + dist(f, f) = 7 + 0 = 7.
Given a string and a distance k. Task is to find a string such that distance of the result string is k from given string. If k distant string is not possible, then print “No”.
Note: There may be exist multiple solutions. We meed to find one of them.
Input : bear k = 26 Output : zcar Here, dist(bear, zcar) = dist(b, z) + dist(e, c) + + dist(a, a) + dist(r, r) = 24 + 2 + 0 + 0 = 26 Input : af k = 7 Output : hf Here, dist(af, hf) = dist(a, h) + dist(f, f) = 7 + 0 = 7 Input : hey k = 1000 Output : No Explaination : No such string exists.
There is no solution if the given required distance is too big. Think what is the maximum possible distance for the given string. Or the more useful thing — how to construct the lost string to maximize the distance? Treat each letter separately and replace it with the most distant letter. For example, we should replace ‘c’ with ‘z’, and we should replace ‘y’ with ‘a’. To be more precise, for first 13 letters of the alphabet the most distant letter is ‘z’, and for other letters it is ‘a’.
The approach is simple, iterate over letters of the given string and greedily change them. A word “greedily” means when changing a letter, don’t care about the next letters. Generally, there must be distant letters, because there may not be a solution otherwise. For each letter of the given string change it into the most distant letter, unless the total distance would be too big. As letters are changed, decrease the remaining required distance. So, for each letter of the given string consider only letters not exceeding the remaining distance, and among them choose the most distant one.
CPP and JAVA Implementation:
- Ways to choose three points with distance between the most distant points <= L
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- String slicing in Python to check if a string can become empty by recursive deletion
- Minimal moves to form a string by adding characters or appending string itself
- Create a new string by alternately combining the characters of two halves of the string in reverse
- String Range Queries to find the number of subsets equal to a given String
- Find the count of palindromic sub-string of a string in its sorted form
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Check if a string can be converted to another string by replacing vowels and consonants
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Minimum number of given operations required to convert a string to another string
- Find the character in first string that is present at minimum index in second string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : nitin mittal