Array ( [0] => [1] => questions [2] => Strings [3] => Transform-one-string-to-another ) Strings | Transform one string to another | THE INQUISITIVE





Transform one string to another

LEVEL:Beginner

Description

Given two strings K and L. You have to find the minimum numerb of operations require to transform one string to another by picking up a character from K and put infront of K, if possible.

Input Format

First line contains the string K.

Output Format

Print minimum number of operations such that to become 2nd string.


Example 1:

Input
abd 
bad
Output
1
Example 2:

Input
eacbd
eabcd
Output
3
Example 3:

Input
abab
baab
Output
1

oops

Login to see Discussion




Approach

step-i) if length of both string is not same return -1
step-ii) assign 0 to answer
step-iii)create a count array of size 256 as total no of characters are 256
step-iv)assign 0 to i
step-v) increment value of count at index equal to ascii value of character at i of first string
step-vi) decrement value of count at index equal to ascii value of character at i of second string
step-vii) traverse the count array and if there are any non zero values return 0
step-viii) assign i and j to string length - 1
step-ix) if ith character of first string is not equal to jth character of second string increment answer else decrement j
step-x) decrement i
step-xi) repeat steps ix and x till i is greater than 0
step-xii) return answer

Time Complexity: O(n)
Space Complexity: O(1)


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution