Find the Lexicographically Largest String From the Box I | Another Approach | Leetcode 3403 | MIK

Find the Lexicographically Largest String From the Box I | Another Approach | Leetcode 3403 | MIK

codestorywithMIK

5 дней назад

5,442 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@VISHU_BADMASH_AAYA
@VISHU_BADMASH_AAYA - 04.06.2025 09:14

Sir what to do when right approach does not hit in my brain i always use the tags and hints to solve the problems and sometimes i am not able to solve the problem after looking at tags and hints both :(

Ответить
@vishwashsoni610
@vishwashsoni610 - 04.06.2025 09:24

Hope your dad feels better soon, sir. Wishing him good health and comfort. ❤❤

Ответить
@AryanVats603
@AryanVats603 - 04.06.2025 09:25

Takecare of your dad sir❤

Ответить
@Nia_naaa
@Nia_naaa - 04.06.2025 09:35

Thank you for this!
I hope your dad gets well soon!🌸

Ответить
@Karan._.00
@Karan._.00 - 04.06.2025 09:38

I hope your dad gets well soon
Solved it by my own.... Here for your explanation 🫡
class Solution {
public:
int n;
void checkLex(string &word,char &lex,int i,int &idx){
int x=idx,y=i;
while(x<n && y<n){
if(word[x+1]!=word[y+1]){
idx=(word[x+1]<word[y+1])?i:idx;
break;
}
x++,y++;
}
}
string answerString(string word, int numFriends) {
n=word.length();

if(numFriends==1) return word;

char lex=word[0];
int idx=0;

for(int i=0;i<n;i++){
if(lex==word[i]){
checkLex(word,lex,i,idx);
}
else if(lex<word[i]){
lex=word[i];
idx=i;
}
}
int x=n-numFriends+1;
return word.substr(idx,x);
}
};

Ответить
@pranjalpandey3077
@pranjalpandey3077 - 04.06.2025 09:41

Take care of your Dad Sir

Ответить
@coderbanda-p2s
@coderbanda-p2s - 04.06.2025 09:48

I have no idea how you manage your time. You are an inspiration.
I pray your dad recovers soon. take care

Ответить
@dayashankarlakhotia4943
@dayashankarlakhotia4943 - 04.06.2025 10:20

I hope you're dad well soon🎉❤

Ответить
@SauravRawat-b9e
@SauravRawat-b9e - 04.06.2025 10:25

Pitaji ke liye prarthna bhai .

Ответить
@Nitian_Jatin
@Nitian_Jatin - 04.06.2025 10:31

it should be n-i+1 , not n-i

Ответить
@krishtripathi9549
@krishtripathi9549 - 04.06.2025 10:37

May Your Father Get well soon 💮

Ответить
@khadeershaik6043
@khadeershaik6043 - 04.06.2025 11:01

Even in tough times, your dedication is inspiring — prayers for your father’s health and thank you for always showing up for us 🙏❤

Ответить
@AbhishekGupta-g8i
@AbhishekGupta-g8i - 04.06.2025 11:30

Take care of your Dad Sir

Ответить
@shubhambrnwl
@shubhambrnwl - 04.06.2025 11:58

Ответить
@shubhambrnwl
@shubhambrnwl - 04.06.2025 11:59

Praying for the wellness of your Father, MIK Sir….

Ответить
@mbbsinaiims1194
@mbbsinaiims1194 - 04.06.2025 12:27

Hope your dad get well soon ❣

Ответить
@adityasikarwar7090
@adityasikarwar7090 - 04.06.2025 12:34

May your Father get well soon ❤❤

Ответить
@adarshpatel9249
@adarshpatel9249 - 04.06.2025 12:37

Wishing him a speedy recovery.

Ответить
@abhijeetkundu3597
@abhijeetkundu3597 - 04.06.2025 12:47

May your father gets well soon

Ответить
@ashwinrao7791
@ashwinrao7791 - 04.06.2025 12:54

Wishing your father a speedy recovery

Ответить
@shaundabre5552
@shaundabre5552 - 04.06.2025 13:17

praying for speedy recovery of your father

Ответить
@powerofnow8166
@powerofnow8166 - 04.06.2025 13:50

Amazing, you are awesome, Thank you very much!
Hope your dad gets well soon!

Ответить
@sauravchandra10
@sauravchandra10 - 04.06.2025 13:55

My wishes to your father for a speedy recovery.

Ответить
@RameezBaigM
@RameezBaigM - 04.06.2025 15:21

Solved it on my own after u said the intuition of size() - friends + 1:
class Solution {
public:
string answerString(string word, int numFriends) {
int n = word.size();
//I can give max (word.size()-numfriends+1) to 1 frnd such tht others get 1 char each
char x = *max_element(word.begin(),word.end());
vector<int> indices;
for(int i = 0; i< n; i++){
if(word[i] == x){
indices.push_back(i);
}
}
if(numFriends == 1){
return word;
}

vector<string> s;
int temp = word.size() - numFriends + 1;
for (int idx : indices) {
int len = min(temp, (int)word.size() - idx);
string sub = word.substr(idx, len);
s.push_back(sub);
}

string res = s[0];
for(int i = 1; i< s.size(); i++){
if(s[i].compare(res) > 0){
res = s[i];
}
}
return res;
}
};

Ответить
@apurvagunnalle1722
@apurvagunnalle1722 - 04.06.2025 15:53

This is how i did it in O(n*n)
Noted down the indices of the largest char in the string
Take the substring from i-n if remaining friends length is less than i
if there are more friends remaining than i, calculate how many remaining and remove that from the end
then take the substr from i
Lets say there are 5 friends , words length is 12, we have found a largest char at index 3, it means index 0,1,2 can be given to other people, but there are total 5 friends so 1 more person is left who will get the last char in word at index 11, so from index [3,10] we have the lexicographically largest word
in this cas eif the total friends were less than index then we take the whole substr from [index,n-1], we repeat this for all indices at which we found the largest char

Ответить
@dipalisharma4682
@dipalisharma4682 - 04.06.2025 17:33

may your father get well soon

Ответить
@DomaHashira
@DomaHashira - 04.06.2025 17:34

Hope your dad get well soon

Ответить
@abirbanerjee839
@abirbanerjee839 - 04.06.2025 18:26

Prayers for our Uncle. Wishing him speedy recovery.

Ответить
@bhuppidhamii
@bhuppidhamii - 04.06.2025 18:31

may god make your father healthy and happy again

Ответить
@SurajGupta-gc9tz
@SurajGupta-gc9tz - 04.06.2025 18:51

please explain the linear solution also

Ответить
@hackathonappsheet7927
@hackathonappsheet7927 - 04.06.2025 18:57

Get well soon uncle ji 🙏🙏

Ответить
@arijitdiganto4166
@arijitdiganto4166 - 04.06.2025 19:14

may your father gets well soon brother. I pray to god for your family's good health

Ответить
@chandan1929
@chandan1929 - 04.06.2025 20:02

I hope you're father is well.

Ответить
@DominiK037
@DominiK037 - 04.06.2025 20:05

Wishing the fast recovery for uncle!!

Stay strong and keep giving us an update on his health <3

Ответить
@susantamandal2782
@susantamandal2782 - 04.06.2025 20:34

Sending positive energies for your father's speedy recovery.

Ответить
@captain-ne8qy
@captain-ne8qy - 04.06.2025 20:53

May Your Father Get well soon
radhe-radhe❤

Ответить
@hareesh_sureddy
@hareesh_sureddy - 04.06.2025 20:58

Nice one MIK ❤️

Ответить
@hikartikey
@hikartikey - 04.06.2025 21:06

Wishing a speedy recovery for your father 🙏🙏

Ответить
@varunpalsingh3822
@varunpalsingh3822 - 04.06.2025 21:11

Uncle will get well soon ❤🙏

Ответить
@Geetanjali-n6g
@Geetanjali-n6g - 04.06.2025 21:11

wishing fast recovery for uncle😇❤

Ответить
@robinhood75t
@robinhood75t - 04.06.2025 21:54

wishing fast recovery for your dad , mate

Ответить
@prasunsingh8179
@prasunsingh8179 - 04.06.2025 22:17

May your dad get well soon sir

Ответить
@official_altymfav
@official_altymfav - 04.06.2025 23:08

why my code is stuck on test case 702 word =
"nbjnc"
numFriends =
2

Use Testcase
Output
"nbjn"
Expected
"nc" =>

class Solution {
public String answerString(String word, int numFriends) {
int n=word.length();

if(numFriends<=1) return word;

int largestCharIdx=0;
int curr=word.charAt(0);
for(int i=1;i<n;i++){
int c=word.charAt(i);

if(c > curr){
largestCharIdx=i;
curr=c;
}

}

int end=Math.min(n, largestCharIdx+(n-numFriends+1));
return word.substring(largestCharIdx,end);
}
}

Ответить
@ZahidLiftUp
@ZahidLiftUp - 05.06.2025 00:13

inshallah he will get well soon

Ответить
@anurag_prajapati12
@anurag_prajapati12 - 05.06.2025 09:14

Ответить
@aayushpaturkar7705
@aayushpaturkar7705 - 05.06.2025 09:55

Wishing fast recovery for your dad , sir!

Ответить
@DuraisamyManikandan-nm3lx
@DuraisamyManikandan-nm3lx - 06.06.2025 08:00

Hats off to you

Ответить
@aakashswastik9458
@aakashswastik9458 - 06.06.2025 20:39

wishing fast recovery for your dad , sir

Ответить
@shashankshekhar2736
@shashankshekhar2736 - 10.06.2025 21:02

how is your father now mike?

Ответить