String Compression III | Leetcode 3163

preview_player
Показать описание
This video explains string compression 3 problem using the most optimal iteration and string manipulation using two pointer approach.
----------------------------------------------------------------------------------------------------------------------------------------------------------------
🟣 JOIN our 𝐋𝐈𝐕𝐄 𝐢𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐭𝐫𝐚𝐢𝐧𝐢𝐧𝐠 𝐩𝐫𝐨𝐠𝐫𝐚𝐦 through whatsapp query: +91 8918633037
---------------------------------------------------------------------------------------------------------------------------------------------------------------

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

Nice explaination, i think we can simplify this even more:
class Solution:
def compressedString(self, word: str) -> str:
n = len(word)
i = 0
res = ""
while i < n:
cnt = 0
start = i
while i < n and word[start] == word[i] and cnt < 9:
i+=1
cnt+=1
res += str(cnt) + word[i-1]
return res

pruthvinarayana
Автор

string compressedString(string word) {
string ans="";
int l=0, r=0;
int count=0;
while(r<word.length()){
if(word[r]!=word[l] || count==9){

l=r;
count=0;
}
count++;
r++;
}
if(l!=word.length()){

}
return ans;
}

amanqureshi