Skip to main content

Featured

Solve LeetCode 30 Substring with Concatenation of All Words in C++ using sliding window and frequency maps

Substring with Concatenation of All Words in C++ LeetCode 30 · sliding window · frequency maps · string_view 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 30. Substring with Concatenation of All Words Given a string s and an array of strings words , where every word has the same length, find all starting indices of substrings that are a concatenation of every word exactly once, in any order. Approach This solution uses a sliding window and two frequency maps. Since every word has the same length, the string is checked in chunks of len characters. freq stores the required frequency of each word. temp stores the frequencies in the current window. count tracks the number of word-sized chunks in the window. If a word occurs too ...

C++ Triangle Pattern Programs 2: Star, Number, Alphabet & Binary Patterns with Output

πŸ”’ C++ Program to Print Continuous Number Triangle Pattern

πŸ“„ Source Code:
#include<iostream>
using namespace std;

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

    cout<<"Enter the number:"<<endl;
    cin>>num;

    for(int i=1;i<=num;i++)
    {
        for(int k=num;k>=i;k--)
        {
            cout<<" ";
        }

        for(int j=1;j<=i;j++)
        {
            cout<<a++<<" ";
        }

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

⭐ C++ Program to Print Star Triangle Pattern

πŸ“„ Source Code:
#include<iostream>
using namespace std;

int main()
{
    int num;

    cout<<"Enter the number:"<<endl;
    cin>>num;

    for(int i=1;i<=num;i++)
    {
        for(int k=num;k>=i;k--)
        {
            cout<<" ";
        }

        for(int j=1;j<=i;j++)
        {
            cout<<"* ";
        }

        cout<<endl;
    }
}
πŸ’» Expected Output (Input: 5):
Enter the number:
5
    * 
   * * 
  * * * 
 * * * * 
* * * * *

πŸ”€ C++ Program to Print Repeated Alphabet Triangle Pattern

πŸ“„ Source Code:
#include<iostream>
using namespace std;

int main()
{
    int num;

    cout<<"Enter the number:"<<endl;
    cin>>num;

    for(int i=1;i<=num;i++)
    {
        for(int k=num;k>=i;k--)
        {
            cout<<" ";
        }

        for(int j=1;j<=i;j++)
        {
            cout<<char(i+64)<<" ";
        }

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

πŸ”€ C++ Program to Print Alphabet Triangle Pattern

πŸ“„ Source Code:
#include<iostream>
using namespace std;

int main()
{
    int num;

    cout<<"Enter the number:"<<endl;
    cin>>num;

    for(int i=1;i<=num;i++)
    {
        for(int k=num;k>=i;k--)
        {
            cout<<" ";
        }

        for(int j=1;j<=i;j++)
        {
            cout<<char(j+64)<<" ";
        }

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

πŸ”’ C++ Program to Print Binary Number Triangle Pattern

πŸ“„ Source Code:
#include<iostream>
using namespace std;

int main()
{
    int num;

    cout<<"Enter the number:"<<endl;
    cin>>num;

    for(int i=1;i<=num;i++)
    {
        for(int k=num;k>=i;k--)
        {
            cout<<" ";
        }

        for(int j=1;j<=i;j++)
        {
            cout<<j%2<<" ";
        }

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

πŸ”’ C++ Program to Print Repeated Binary Triangle Pattern

πŸ“„ Source Code:
#include<iostream>
using namespace std;

int main()
{
    int num;

    cout<<"Enter the number:"<<endl;
    cin>>num;

    for(int i=1;i<=num;i++)
    {
        for(int k=num;k>=i;k--)
        {
            cout<<" ";
        }

        for(int j=1;j<=i;j++)
        {
            cout<<i%2<<" ";
        }

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

πŸ”’ C++ Program to Print Repeated Number Triangle Pattern

πŸ“„ Source Code:
#include<iostream>
using namespace std;

int main()
{
    int num;

    cout<<"Enter the number:"<<endl;
    cin>>num;

    for(int i=1;i<=num;i++)
    {
        for(int k=num;k>=i;k--)
        {
            cout<<" ";
        }

        for(int j=1;j<=i;j++)
        {
            cout<<i<<" ";
        }

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

πŸ”’ C++ Program to Print Number Triangle Pattern

πŸ“„ Source Code:
#include<iostream>
using namespace std;

int main()
{
    int num;

    cout<<"Enter the number:"<<endl;
    cin>>num;

    for(int i=1;i<=num;i++)
    {
        for(int k=num;k>=i;k--)
        {
            cout<<" ";
        }

        for(int j=1;j<=i;j++)
        {
            cout<<j<<" ";
        }

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

⭐ C++ Program to Print Palindromic Alphabet Triangle Pattern

πŸ“„ Source Code:
#include<iostream>
using namespace std;

int main()
{
    int num;

    cout << "Enter the number:" << endl;
    cin >> num;

    for(int i = 1; i <= num; i++)
    {
        // Print spaces
        for(int k = num; k >= i; k--)
        {
            cout << " ";
        }

        // Print increasing alphabets
        for(int j = 1; j <= i; j++)
        {
            cout << char(j + 64) << " ";
        }

        // Print decreasing alphabets
        for(int j = i - 1; j >= 1; j--)
        {
            cout << char(j + 64) << " ";
        }

        cout << endl;
    }

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

     A
    A B A
   A B C B A
  A B C D C B A
 A B C D E D C B A

πŸ“˜ Explanation

This C++ program prints a palindromic alphabet triangle pattern using nested for loops. The outer loop controls the number of rows, while the first inner loop prints spaces to create the required alignment.

The second inner loop prints alphabets in increasing order from A to the current character. The third inner loop prints the alphabets in decreasing order, creating a mirror image of the first half. Therefore, each row forms a palindrome such as A B C B A.

The expression char(j + 64) converts an integer value into its corresponding uppercase alphabet using ASCII values. For example, 1 + 64 = 65, which represents A, while 2 + 64 = 66, which represents B.

🧠 Algorithm

  1. Start the program.
  2. Read the number of rows from the user.
  3. Run an outer loop from i = 1 to num.
  4. Print spaces before the alphabets to create the required alignment.
  5. Print alphabets from A to the current character using an increasing loop.
  6. Print the alphabets in reverse order using a decreasing loop.
  7. Move to the next line after completing each row.
  8. Repeat until all rows are printed.
  9. Stop the program.

πŸ’‘ Key Concept

The pattern is created using two alphabet loops. The first loop prints characters in increasing order, while the second loop prints them in decreasing order.

Increasing: A B C
Decreasing: B A

Final Row: A B C B A

In modern C++, the alphabet conversion can also be written more clearly as:

cout << char('A' + j - 1) << " ";

πŸ”„ C to C++ Conversion

The main changes when converting this pattern program from C to C++ are:

C                         C++

#include <stdio.h>        #include <iostream>

printf()                  cout

scanf()                   cin

%c                        char()

return 0;                 return 0;

The important character conversion changes from:

printf("%c ", j + 64);

⬇️

cout << char(j + 64) << " ";

⏱ Time Complexity

O(n²) — The program uses nested loops to print spaces and characters for every row. The total number of operations grows approximately quadratically with the number of rows.

πŸ’Ύ Space Complexity

O(1) — The program uses only a fixed number of variables and does not require additional memory proportional to the input size.

πŸ“ Search Description

Learn how to print a palindromic alphabet triangle pattern in C++ using nested loops. This beginner-friendly C++ program prints alphabets in increasing and decreasing order to create a symmetric character pattern. It also explains ASCII character conversion and the difference between C and C++ input/output syntax.

πŸ” Keywords

palindromic alphabet triangle pattern in C++, C++ alphabet palindrome pattern, alphabet triangle pattern in C++, C++ palindrome pattern program, character palindrome pattern, alphabet pattern using nested loops, C++ pattern programs, ASCII alphabet pattern in C++, C to C++ conversion, nested loops in C++, C++ programming examples

🏷 Hashtags

#CppProgramming #PatternPrograms #AlphabetPattern #PalindromePattern #CharacterPattern #TrianglePattern #NestedLoops #ASCII #CPlusPlus #Coding #Programming #DSA #1printf

Comments

Popular Posts

πŸŒ™