Minimum Length of String After Operations | Leetcode 3223

preview_player
Показать описание
This video explains Minimum Length of String After Operations using the most optimal count approach.
----------------------------------------------------------------------------------------------------------------------------------------------------------------
🟣 JOIN our 𝐋𝐈𝐕𝐄 𝐢𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐭𝐫𝐚𝐢𝐧𝐢𝐧𝐠 𝐩𝐫𝐨𝐠𝐫𝐚𝐦 through whatsapp query: +91 8918633037
---------------------------------------------------------------------------------------------------------------------------------------------------------------

Рекомендации по теме
Комментарии
Автор

After this intuition 6:04 its so much easy to code .. i used unordered map to solve the problem
Thank you so much sir :)

sailendrachettri
Автор

Excellent explanations, easy to understand and follow through. Please consider adding Python3 solution too.

tahamidhossain
Автор

Top Class! Finding this pattern is really awesome! Great Explanation!

christofrank
Автор

class Solution:
def minimumLength(self, s: str) -> int:
counter = Counter(s)
for letter in counter:
if counter[letter] % 2 == 0:
counter[letter] = 2
else:
counter[letter] = 1

res = 0
for letter in counter:
res += counter[letter]

return res

albin_joby
Автор

I too solved the problem, but with a different approach when a character hits the freqency of 3 then it is capable of having two guys on both the directions so we -2 from the string length and thats out answer


code :


class Solution {
public int minimumLength(String s) {

char arr[]=s.toCharArray();

HashMap<Character, Integer>mp=new HashMap<>();

int ans=s.length();

for(int i=0;i<arr.length;i++)
{
int freq=0;

if(mp.containsKey(arr[i]))
{
freq=mp.get(arr[i]);
}

if(freq+1==3)
{
ans-=2;
mp.put(arr[i], 1);
}
else
{
mp.put(arr[i], freq+1);
}
}

return ans;

}
}

wierdo
Автор

I tried but reached TLE and done almost 693 / 702 test case. This explanation is very useful class Solution {

public boolean check(HashMap<Character, Integer> map){
for (int count : map.values()) {
if (count >= 3) {
return true;
}
}
return false;
}
public int minimumLength(String s) {
StringBuilder sb = new StringBuilder(s);
int n = s.length();
HashMap<Character, Integer> map = new HashMap<>();
for(int i=0;i<n;i++){
char ch = s.charAt(i);
map.put(ch, map.getOrDefault(ch, 0)+1);
}
while(check(map)){
for(char key : map.keySet()){
int val = map.get(key);
if(val >= 3){
int left_index = sb.indexOf(key+"");
int right_index = sb.lastIndexOf(key+"");
if(left_index != -1 && right_index != -1 && left_index != right_index){
sb.delete(left_index, left_index+1);
sb.delete(right_index-1, right_index);
}
map.put(key, map.get(key) - 2);
}
}
}
return sb.length();
}
}

cheedallatejasrinivas
Автор

How do you upload a new problem every single day??? Please tell us how to be consistent with Leetcode along with a job!

chocolateandmath
Автор

Hi sir. I don't understand how are you analysing the constraints at the start of every video. It's very confusing sir, analysing the time constraints

mr.haristotle
join shbcf.ru