Reverse Prefix of Word (LeetCode 2000) | Full Solution with stack data structure

Поделиться
HTML-код
  • Опубликовано: 26 ноя 2024

Комментарии • 11

  • @IshaZaka
    @IshaZaka 2 месяца назад

    Hi Nikhil, plz make for hard challegnes as well

  • @AliRaza-gh3hp
    @AliRaza-gh3hp 6 месяцев назад

    Brother You are doing awesome work for students like us please keep going. can you please do the problem 6. Zizag Conversion on leetcode. I will be thankful.

  • @unemployedcse3514
    @unemployedcse3514 6 месяцев назад

    awesome ❤

  • @akankshasonkar6846
    @akankshasonkar6846 6 месяцев назад +1

    Nice solution Nikhil :)

  • @rdrahuldhiman19
    @rdrahuldhiman19 6 месяцев назад

    I hope your eye heals quickly

    • @nikoo28
      @nikoo28  6 месяцев назад

      thanks a lot for the good wishes...the eye infection caused me some delay to publish videos. Thanks for being a viewer of my channel and your patience :)

  • @arupgope2676
    @arupgope2676 3 месяца назад

    I solve it using 2 pointer

  • @hemanthcse1
    @hemanthcse1 6 месяцев назад

    We can achieve this without using extra space like stack
    fun reversePrefixSolution3(word: String, ch: Char): String {
    val firstOccurrence = word.indexOf(ch)
    if (firstOccurrence == -1){
    return word
    }
    val result = CharArray(word.length)
    for (i in 0..firstOccurrence){
    result[i] = word[firstOccurrence-i]
    }
    for (i in (firstOccurrence+1) until word.length){
    result[i] = word[i]
    }
    return String(result)
    }
    correct me if i am wrong

    • @nikoo28
      @nikoo28  6 месяцев назад

      When you use the charArray it does take up extra O(n) space. So the time complexity remains the same

  • @anuragrawat4350
    @anuragrawat4350 6 месяцев назад

    int index = word.indexOf(ch)
    if(index != -1){
    return new StringBuilder(word.substring(0,index+1).reverse).toString() + word(substring(index+1,word.length()));
    }
    return word;

  • @RameshGaridapuri
    @RameshGaridapuri 6 месяцев назад

    // Online C compiler to run C program online
    #include
    #include
    void swap(char *p, char *q)
    {
    int temp;
    while ( p < q)
    {
    temp = *p ;
    *p++ = *q;
    *q-- = temp;
    }

    }
    char * fun_ub(char arr[], char p , int len)
    {
    int i = 0 ;
    while ( arr[i]!= '\0')
    {
    if ( arr[i] == 'd')
    {
    swap( &arr[0], &arr[i]);
    break;
    }
    i++;
    }
    return arr;
    }
    int main() {
    char arr[] = "abcdefd";
    char a = 'd';
    int stl = strlen(arr)-1;
    char *p = fun_ub(arr, a , stl);
    printf("%s", p);
    }