6 Identity Verification Methods

Identity is a person’s most essential and most used asset. Identity Verification is to prove that the identity of the person they claim to have is matched with their truth. It measures whether a…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Get Equal Substrings Within Budget

You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.

You are also given an integer maxCost.

Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.

If there is no substring from s that can be changed to its corresponding substring from t, return 0.

Example 1:

Example 2:

Example 3:

Constraints:

Solution:

The trick is to find the difference between the ASCII value of two characters from s and t. After, then it becomes a sliding window problem.

To avoid O(n²), we can keep a pointer called ‘i’.

Whenever we find maxcost value is <0, we will update the ‘i’ and add the value for the position of ‘i’ with max cost. Why add? Because when we iterate we subtract from maxcost, we have to add for removing the impact.

Here is my java solution:

Add a comment

Related posts:

NEED FOR PUBLIC HEALTHCARE REFORMS IN INDIA

There is justifiable anger at the recent deaths of children in a UP hospital. These deaths are undoubtedly a case of criminal negligence of the Hospital authorities and those in Government who are…

Norway to Dover

Captain Henry Hammond Shott DSO was, by all reports, an extraordinary man. Not only did he survive the Boer War and various African uprisings, he also learned to fly a Bristol Biplane shortly before…

Casino night hire auckland

Fun Casino Night Hire A fun casino night hire is a great way to relax and have a great time with colleagues and friends. Select from our various event theming ideas to compliment your casino evening…