1.
Simple Triangle Pattern
**
***
****
*****
using System;
class Program
static void Main()
int n = 5; // Number of rows
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++)
Console.Write("*");
Console.WriteLine();
}
Inverted Triangle Pattern
*****
****
***
**
using System;
class Program
static void Main()
int n = 5; // Number of rows
for (int i = n; i >= 1; i--)
for (int j = 1; j <= i; j++)
Console.Write("*");
Console.WriteLine();
}
Pyramid Pattern
***
*****
*******
*********
using System;
class Program
static void Main()
int n = 5; // Number of rows
for (int i = 1; i <= n; i++)
for (int j = i; j < n; j++)
Console.Write(" ");
for (int k = 1; k <= (2 * i - 1); k++)
Console.Write("*");
Console.WriteLine();
}
Diamond Pattern
***
*****
*******
*********
*******
*****
***
using System;
class Program
static void Main()
int n = 5; // Number of rows
for (int i = 1; i <= n; i++)
for (int j = i; j < n; j++)
Console.Write(" ");
for (int k = 1; k <= (2 * i - 1); k++)
Console.Write("*");
Console.WriteLine();
for (int i = n - 1; i >= 1; i--)
{
for (int j = n; j > i; j--)
Console.Write(" ");
for (int k = 1; k <= (2 * i - 1); k++)
Console.Write("*");
Console.WriteLine();
Number Pyramid Pattern
222
33333
4444444
555555555
using System;
class Program
static void Main()
int n = 5; // Number of rows
for (int i = 1; i <= n; i++)
for (int j = i; j < n; j++)
Console.Write(" ");
}
for (int k = 1; k <= (2 * i - 1); k++)
Console.Write(i);
Console.WriteLine();
Floyd's Triangle
23
456
7 8 9 10
11 12 13 14 15
using System;
class Program
static void Main()
int n = 5; // Number of rows
int num = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++)
Console.Write(num + " ");
num++;
Console.WriteLine();
}
Pascal's Triangle
11
121
1331
14641
using System;
class Program
static void Main()
int n = 5; // Number of rows
int[,] pascal = new int[n, n];
for (int line = 0; line < n; line++)
for (int i = 0; i <= line; i++)
if (line == i || i == 0)
pascal[line, i] = 1;
else
pascal[line, i] = pascal[line - 1, i - 1] + pascal[line - 1, i];
Console.Write(pascal[line, i] + " ");
Console.WriteLine();
}
Number Triangle Pattern
22
333
4444
55555
using System;
class Program
static void Main()
int n = 5; // Number of rows
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++)
Console.Write(i);
Console.WriteLine();
}
Alphabet Triangle Pattern
BB
CCC
DDDD
EEEEE
using System;
class Program
static void Main()
int n = 5; // Number of rows
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++)
Console.Write((char)('A' + i - 1));
Console.WriteLine();
}
Hollow Square Pattern
*****
* *
* *
* *
*****
using System;
class Program
static void Main()
int n = 5; // Size of the square
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i == 1 || i == n || j == 1 || j == n)
Console.Write("*");
else
Console.Write(" ");
Console.WriteLine();
}
Hollow Diamond Pattern
**
* *
* *
* *
* *
* *
**
using System;
class Program
static void Main()
int n = 5; // Number of rows
for (int i = 1; i <= n; i++)
for (int j = i; j < n; j++)
Console.Write(" ");
for (int k = 1; k <= (2 * i - 1); k++)
if (k == 1 || k == (2 * i - 1))
Console.Write("*");
else
Console.Write(" ");
Console.WriteLine();
}
for (int i = n - 1; i >= 1; i--)
for (int j = n; j > i; j--)
Console.Write(" ");
for (int k = 1; k <= (2 * i - 1); k++)
if (k == 1 || k == (2 * i - 1))
Console.Write("*");
else
Console.Write(" ");
Console.WriteLine();
Zig-Zag Pattern
* * *
* ** **
** ** **
using System;
class Program
static void Main()
int n = 3; // Height of the zig-zag pattern
int m = 9; // Width of the zig-zag pattern
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if ((i + j) % 4 == 0 || (i == 2 && j % 4 == 0))
Console.Write("*");
else
Console.Write(" ");
Console.WriteLine();
Butterfly Pattern
* *
** **
*** ***
**** ****
*********
*********
**** ****
*** ***
** **
* *
using System;
class Program
static void Main()
{
int n = 5; // Number of rows for each half
// Upper half
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++)
Console.Write("*");
for (int j = 1; j <= 2 * (n - i); j++)
Console.Write(" ");
for (int j = 1; j <= i; j++)
Console.Write("*");
Console.WriteLine();
// Lower half
for (int i = n; i >= 1; i--)
for (int j = 1; j <= i; j++)
Console.Write("*");
for (int j = 1; j <= 2 * (n - i); j++)
Console.Write(" ");
}
for (int j = 1; j <= i; j++)
Console.Write("*");
Console.WriteLine();
Hourglass Pattern
*********
*******
*****
***
***
*****
*******
*********
using System;
class Program
static void Main()
int n = 5; // Number of rows for each half
// Upper half
for (int i = n; i >= 1; i--)
{
for (int j = i; j < n; j++)
Console.Write(" ");
for (int j = 1; j <= (2 * i - 1); j++)
Console.Write("*");
Console.WriteLine();
// Lower half
for (int i = 2; i <= n; i++)
for (int j = i; j < n; j++)
Console.Write(" ");
for (int j = 1; j <= (2 * i - 1); j++)
Console.Write("*");
Console.WriteLine();
}
Hollow Pyramid Pattern
**
* *
* *
*********
using System;
class Program
static void Main()
int n = 5; // Number of rows
for (int i = 1; i <= n; i++)
for (int j = i; j < n; j++)
Console.Write(" ");
for (int k = 1; k <= (2 * i - 1); k++)
if (k == 1 || k == (2 * i - 1) || i == n)
Console.Write("*");
else
Console.Write(" ");
Console.WriteLine();
}
Reverse a String
using System;
class Program
static void Main()
string input = "hello";
string reversed = ReverseString(input);
Console.WriteLine(reversed); // Output: "olleho"
static string ReverseString(string str)
char[] charArray = str.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
Check if a String is a Palindrome
using System;
class Program
static void Main()
string input = "madam";
bool isPalindrome = IsPalindrome(input);
Console.WriteLine(isPalindrome); // Output: True
}
static bool IsPalindrome(string str)
int left = 0;
int right = str.Length - 1;
while (left < right)
if (str[left] != str[right])
return false;
left++;
right--;
return true;
Count the Number of Vowels in a String
using System;
class Program
static void Main()
string input = "hello world";
int vowelCount = CountVowels(input);
Console.WriteLine(vowelCount); // Output: 3
static int CountVowels(string str)
{
int count = 0;
string vowels = "aeiouAEIOU";
foreach (char c in str)
if (vowels.Contains(c))
count++;
return count;
Find the First Non-Repeated Character in a String
using System;
using System.Collections.Generic;
class Program
static void Main()
string input = "swiss";
char result = FirstNonRepeatedCharacter(input);
Console.WriteLine(result); // Output: w
static char FirstNonRepeatedCharacter(string str)
Dictionary<char, int> charCount = new Dictionary<char, int>();
foreach (char c in str)
if (charCount.ContainsKey(c))
charCount[c]++;
else
charCount[c] = 1;
foreach (char c in str)
if (charCount[c] == 1)
return c;
return '\0'; // Return null character if no unique character is found
Check if Two Strings are Anagrams
using System;
class Program
static void Main()
string str1 = "listen";
string str2 = "silent";
bool areAnagrams = AreAnagrams(str1, str2);
Console.WriteLine(areAnagrams); // Output: True
}
static bool AreAnagrams(string str1, string str2)
if (str1.Length != str2.Length)
return false;
char[] charArray1 = str1.ToCharArray();
char[] charArray2 = str2.ToCharArray();
Array.Sort(charArray1);
Array.Sort(charArray2);
for (int i = 0; i < charArray1.Length; i++)
if (charArray1[i] != charArray2[i])
return false;
return true;
Longest Substring Without Repeating Characters
using System;
using System.Collections.Generic;
class Program
static void Main()
string input = "abcabcbb";
int length = LengthOfLongestSubstring(input);
Console.WriteLine(length); // Output: 3
static int LengthOfLongestSubstring(string s)
int maxLength = 0;
int start = 0;
Dictionary<char, int> seen = new Dictionary<char, int>();
for (int end = 0; end < s.Length; end++)
char currentChar = s[end];
if (seen.ContainsKey(currentChar))
start = Math.Max(seen[currentChar] + 1, start);
seen[currentChar] = end;
maxLength = Math.Max(maxLength, end - start + 1);
return maxLength;
}
String Compression
using System;
using System.Text;
class Program
static void Main()
string input = "aabcccccaaa";
string compressed = CompressString(input);
Console.WriteLine(compressed); // Output: a2b1c5a3
static string CompressString(string str)
StringBuilder sb = new StringBuilder();
int count = 1;
for (int i = 0; i < str.Length; i++)
if (i + 1 < str.Length && str[i] == str[i + 1])
count++;
else
sb.Append(str[i]);
sb.Append(count);
count = 1;
}
return sb.ToString();
Check if a String is a Rotation of Another String
using System;
class Program
static void Main()
string str1 = "waterbottle";
string str2 = "erbottlewat";
bool isRotation = IsRotation(str1, str2);
Console.WriteLine(isRotation); // Output: True
static bool IsRotation(string s1, string s2)
if (s1.Length != s2.Length)
return false;
string concatenated = s1 + s1;
return concatenated.Contains(s2);
}
Remove Duplicates from a String
using System;
using System.Collections.Generic;
using System.Text;
class Program
static void Main()
string input = "programming";
string result = RemoveDuplicates(input);
Console.WriteLine(result); // Output: "progamin"
static string RemoveDuplicates(string str)
HashSet<char> seen = new HashSet<char>();
StringBuilder sb = new StringBuilder();
foreach (char c in str)
if (!seen.Contains(c))
seen.Add(c);
sb.Append(c);
return sb.ToString();
}
Find the Longest Common Prefix
using System;
class Program
static void Main()
string[] strs = { "flower", "flow", "flight" };
string result = LongestCommonPrefix(strs);
Console.WriteLine(result); // Output: "fl"
static string LongestCommonPrefix(string[] strs)
if (strs == null || strs.Length == 0)
return "";
string prefix = strs[0];
for (int i = 1; i < strs.Length; i++)
while (strs[i].IndexOf(prefix) != 0)
prefix = prefix.Substring(0, prefix.Length - 1);
if (prefix == "")
return "";
return prefix;
}
Group Anagrams
using System;
using System.Collections.Generic;
class Program
static void Main()
string[] strs = { "eat", "tea", "tan", "ate", "nat", "bat" };
var result = GroupAnagrams(strs);
foreach (var group in result)
Console.WriteLine(string.Join(", ", group));
// Output: ["eat", "tea", "ate"], ["tan", "nat"], ["bat"]
static IList<IList<string>> GroupAnagrams(string[] strs)
Dictionary<string, List<string>> map = new Dictionary<string, List<string>>();
foreach (string str in strs)
char[] charArray = str.ToCharArray();
Array.Sort(charArray);
string sortedStr = new string(charArray);
if (!map.ContainsKey(sortedStr))
map[sortedStr] = new List<string>();
}
map[sortedStr].Add(str);
return new List<IList<string>>(map.Values);
Find the First Unique Character in a String
using System;
using System.Collections.Generic;
class Program
static void Main()
string input = "leetcode";
int result = FirstUniqueCharacter(input);
Console.WriteLine(result); // Output: 0
static int FirstUniqueCharacter(string s)
Dictionary<char, int> charCount = new Dictionary<char, int>();
foreach (char c in s)
if (charCount.ContainsKey(c))
charCount[c]++;
else
charCount[c] = 1;
}
for (int i = 0; i < s.Length; i++)
if (charCount[s[i]] == 1)
return i;
return -1;
Implement strStr() (Needle in a Haystack)
using System;
class Program
static void Main()
string haystack = "hello";
string needle = "ll";
int result = StrStr(haystack, needle);
Console.WriteLine(result); // Output: 2
static int StrStr(string haystack, string needle)
if (string.IsNullOrEmpty(needle))
return 0;
for (int i = 0; i <= haystack.Length - needle.Length; i++)
{
if (haystack.Substring(i, needle.Length) == needle)
return i;
return -1;
Longest Palindromic Substring
using System;
class Program
static void Main()
string input = "babad";
string result = LongestPalindrome(input);
Console.WriteLine(result); // Output: "bab" or "aba"
static string LongestPalindrome(string s)
if (string.IsNullOrEmpty(s)) return "";
int start = 0, maxLength = 1;
for (int i = 0; i < s.Length; i++)
int low = i - 1;
int high = i + 1;
while (high < s.Length && s[high] == s[i]) high++;
while (low >= 0 && s[low] == s[i]) low--;
while (low >= 0 && high < s.Length && s[low] == s[high])
low--;
high++;
int length = high - low - 1;
if (length > maxLength)
start = low + 1;
maxLength = length;
return s.Substring(start, maxLength);
Valid Parentheses
using System;
using System.Collections.Generic;
class Program
static void Main()
string input = "{[]}";
bool result = IsValid(input);
Console.WriteLine(result); // Output: True
static bool IsValid(string s)
Stack<char> stack = new Stack<char>();
foreach (char c in s)
if (c == '(' || c == '{' || c == '[')
stack.Push(c);
else
if (stack.Count == 0) return false;
char top = stack.Pop();
if ((c == ')' && top != '(') ||
(c == '}' && top != '{') ||
(c == ']' && top != '['))
return false;
return stack.Count == 0;