Skip to main content

Featured

Solve LeetCode 3 Longest Substring Without Repeating Characters in C++ using the sliding window

Longest Substring Without Repeating Characters in C++ LeetCode 3 · sliding window · last index array Table of Contents Problem Statement Approach Algorithm C++ Program Input Sample Output Output Explanation Dry Run Flow of Execution Time Complexity Space Complexity Applications Key Points Interview Questions Frequently Asked Questions Conclusion Problem Statement 3. Longest Substring Without Repeating Characters Given a string s , find the length of the longest substring that contains no repeating characters. A substring is a continuous part of the string, so "pwke" is a subsequence of "pwwkew" but not a substring. Approach We use a sliding window . The window is the part of the string between start and end , and we always keep it free of repeated characters. An array lastIndex of size 128 remembers the most recent position of every ASCII ...

Mastering Hollow Square Patterns in C: Stars, Numbers, Alphabets & Binary

πŸ”’ C Program to Print Hollow Continuous Number Square

πŸ“„ Source Code:
#include <stdio.h>

int main()
{
    int num, k = 0;

    printf("Enter the number:\n");
    scanf("%d", &num);

    for(int i = 1; i <= num; i++)
    {
        for(int j = 1; j <= num; j++)
        {
            if(i == 1 || i == num || j == 1 || j == num)
            {
                // k increments sequentially only along the borders
                printf("%d ", k++);
            }
            else
            {
                printf("  ");
            }
        }
        printf("\n"); 
    }

    return 0;
}
  
πŸ’» Expected Output (Input: 5):
Enter the number:
5
0 1 2 3 4 
5       6 
7       8 
9       10 
11 12 13 14 15 
  

πŸ”’ C Program to Print Standard Hollow Binary Row Square

πŸ“„ Source Code (Fixed Specifier):
#include <stdio.h>

int main()
{
    int num;

    printf("Enter the number:\n");
    scanf("%d", &num);

    for(int i = 1; i <= num; i++)
    {
        for(int j = 1; j <= num; j++)
        {
            if(i == 1 || i == num || j == 1 || j == num)
            {
                // Fixed: Changed %c to %d to correctly display the binary digits
                printf("%d ", i % 2);
            }
            else
            {
                printf("  ");
            }
        }
        printf("\n"); 
    }

    return 0;
}
  
πŸ’» Expected Output (Input: 5):
Enter the number:
5
1 1 1 1 1 
0       0 
1       1 
0       0 
1 1 1 1 1 
  

πŸ”’ C Program to Print Standard Hollow Binary Column Square

πŸ“„ Source Code (Fixed Specifier):
#include <stdio.h>

int main()
{
    int num;

    printf("Enter the number:\n");
    scanf("%d", &num);

    for(int i = 1; i <= num; i++)
    {
        for(int j = 1; j <= num; j++)
        {
            if(i == 1 || i == num || j == 1 || j == num)
            {
                // Fixed: Changed %c to %d to correctly display the binary digits
                printf("%d ", j % 2);
            }
            else
            {
                printf("  ");
            }
        }
        printf("\n"); 
    }

    return 0;
}
  
πŸ’» Expected Output (Input: 5):
Enter the number:
5
1 0 1 0 1 
1       1 
1       1 
1       1 
1 0 1 0 1 
  

πŸ”’ C Program to Print Inverted Hollow Row-Alphabet Square

πŸ“„ Source Code:
#include <stdio.h>

int main()
{
    int num;

    printf("Enter the number:\n");
    scanf("%d", &num);

    for(int i = num; i >= 1; i--)
    {
        for(int j = num; j >= 1; j--)
        {
            if(i == 1 || i == num || j == 1 || j == num)
            {
                // Prints character based on the inverted row index (i)
                printf("%c ", i + 64);
            }
            else
            {
                printf("  ");
            }
        }
        printf("\n"); 
    }

    return 0;
}
  
πŸ’» Expected Output (Input: 5):
Enter the number:
5
E E E E E 
D       D 
C       C 
B       B 
A A A A A 
  

πŸ”’ C Program to Print Inverted Hollow Alphabet Square Pattern

πŸ“„ Source Code:
#include <stdio.h>

int main()
{
    int num;

    printf("Enter the number:\n");
    scanf("%d", &num);

    for(int i = num; i >= 1; i--)
    {
        for(int j = num; j >= 1; j--)
        {
            if(i == 1 || i == num || j == 1 || j == num)
            {
                printf("%c ", j + 64);
            }
            else
            {
                printf("  ");
            }
        }
        printf("\n"); 
    }

    return 0;
}
  
πŸ’» Expected Output (Input: 5):
Enter the number:
5
E D C B A 
E       A 
E       A 
E       A 
E D C B A 
  

πŸ”’ C Program to Print Hollow Star Square Pattern

πŸ“„ Source Code:
#include <stdio.h>

int main()
{
    int num;

    printf("Enter the number:\n");
    scanf("%d", &num);

    for(int i = 1; i <= num; i++)
    {
        for(int j = 1; j <= num; j++)
        {
            if(i == 1 || i == num || j == 1 || j == num)
            {
                printf("* ");
            }
            else
            {
                printf("  ");
            }
        }
        printf("\n");
    }

    return 0;
}
  
πŸ’» Expected Output (Input: 5):
Enter the number:
5
* * * * *
*       *
*       *
*       *
* * * * *
  

πŸ”’ C Program to Print Hollow Row-Alphabet Square Pattern

πŸ“„ Source Code:
#include <stdio.h>

int main()
{
    int num;

    printf("Enter the number:\n");
    scanf("%d", &num);

    for(int i = 1; i <= num; i++)
    {
        for(int j = 1; j <= num; j++)
        {
            if(i == 1 || i == num || j == 1 || j == num)
            {
                // Prints character based on row index (i)
                printf("%c ", i + 64);
            }
            else
            {
                printf("  ");
            }
        }
        printf("\n"); 
    }

    return 0;
}
  
πŸ’» Expected Output (Input: 5):
Enter the number:
5
A A A A A 
B       B 
C       C 
D       D 
E E E E E 
  

πŸ”’ C Program to Print Hollow Alphabet Square Pattern

πŸ“„ Source Code:
#include <stdio.h>

int main()
{
    int num;

    printf("Enter the number:\n");
    scanf("%d", &num);

    for(int i = 1; i <= num; i++)
    {
        for(int j = 1; j <= num; j++)
        {
            if(i == 1 || i == num || j == 1 || j == num)
            {
                printf("%c ", j + 64);
            }
            else
            {
                printf("  ");
            }
        }
        printf("\n"); 
    }

    return 0;
}
  
πŸ’» Expected Output (Input: 5):
Enter the number:
5
A B C D E 
A       E 
A       E 
A       E 
A B C D E 
  

πŸ”’ C Program to Print Inverted Hollow Number Square Pattern

πŸ“„ Source Code:
#include <stdio.h>

int main()
{
    int num;

    printf("Enter the number:\n");
    scanf("%d", &num);

    for(int i = num; i >= 1; i--)
    {
        for(int j = num; j >= 1; j--)
        {
            if(i == 1 || i == num || j == 1 || j == num)
            {
                printf("%d ", j);
            }
            else
            {
                printf("  ");
            }
        }
        printf("\n"); 
    }

    return 0;
}
  
πŸ’» Expected Output (Input: 5):
Enter the number:
5
5 4 3 2 1 
5       1 
5       1 
5       1 
5 4 3 2 1 
  

πŸ”’ C Program to Print Hollow Number Square Pattern

πŸ“„ Source Code:
#include <stdio.h>

int main()
{
    int num;

    printf("Enter the number:\n");
    scanf("%d", &num);

    for(int i = 1; i <= num; i++)
    {
        for(int j = 1; j <= num; j++)
        {
            if(i == 1 || i == num || j == 1 || j == num)
            {
                printf("%d ", j);
            }
            else
            {
                printf("  ");
            }
        }
        printf("\n"); 
    }

    return 0;
}
  
πŸ’» Expected Output (Input: 5):
Enter the number:
5
1 2 3 4 5 
1       5 
1       5 
1       5 
1 2 3 4 5 
  

Comments

Popular Posts

πŸŒ™