Leetcode 1544. Make String Great
I am a developer from Nashville, TN. I specialize in the .NET tech stack. I have created many projects in Blazor WASM, Xamarin, MAUI, etc.
Search for a command to run...
I am a developer from Nashville, TN. I specialize in the .NET tech stack. I have created many projects in Blazor WASM, Xamarin, MAUI, etc.
No comments yet. Be the first to comment.
A series documenting my journey to improving my ability to solve LeetCode problems through YouTube videos, study plans, articles, etc. Using my own words for later reference.
Intuition Since both arrays are sorted, we can find the minimum common value by iterating from left to right with two pointers. Approach Initialize both pointers to 0 to start at the beginning of the array. While both pointers are within bounds of th...
Intuition Get the length of the subsequence (if any) and subtract it from the length of t to find out how many characters we have to append (if any) Approach Keep a count of the length of the subsequence while iterating through s and then subtract th...
Intuition Sort the array and check if the start and end of the array match. If not, increment or decrement accordingly. Approach First sort the array. Then use two pointers. One at the beginning, the other at the end. Check to see if the value at the...
Intuition Find position of ch (if any) then use two pointers to swap characters until string is reveresed from beginning to index of ch Approach Start by checking each character for ch. If we find it, that's where our right pointer will start. Our le...
Intuition Since both arrays are sorted, we can find the minimum common value by iterating from left to right with two pointers. Approach Initialize both pointers to 0 to start at the beginning of the array. While both pointers are within bounds of th...
Use two pointers to iterate through the string checking each adjacent character to see if it is "bad" or "good".
First if the string is 1 or fewer characters we know it is good so we can just return it.
If that's not the case, we change it to an array and then to a list so we can dynamically resize it.
Then while p2 is less than the count of the list, we just iterate through and using the ASCII values, check to see if the two characters are bad. If they are, we remove them from the list and reset out pointers. If they are good, we just increment the pointers.
Finally when we reach the end of the list we return the remaining string.
public class Solution
{
public string MakeGood(string s)
{
if(s.Length == 0 || s.Length == 1)
{
return s;
}
//convert string to list
var arr = s.ToCharArray();
var list = new List<char>(arr);
//iterate through string with 2 pointers
var p1 = 0;
var p2 = 1;
while(p2 < list.Count)
{
var num1 = list[p1];
var num2 = list[p2];
//check if adjacent characters are bad
if(Math.Abs(num1 - num2) == 32)
{
//if so remove them and reset pointers
list.RemoveRange(p1, 2);
p1 = 0;
p2 = 1;
}
else
{
p1 = p2;
p2++;
}
}
return new string(list.ToArray());
}
}