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.
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 :)
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
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;
// 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); }
Hi Nikhil, plz make for hard challegnes as well
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.
awesome ❤
Nice solution Nikhil :)
I hope your eye heals quickly
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 :)
I solve it using 2 pointer
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
When you use the charArray it does take up extra O(n) space. So the time complexity remains the same
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;
// 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);
}