-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
152 lines (106 loc) · 3.35 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// See https://aka.ms/new-console-template for more information
//Console.WriteLine("Hello, World!");
/* Example 1: Given a string s, return true if it is a palindrome, false otherwise.
A string is a palindrome if it reads the same forward as backward. That means, after reversing it, it is still the same string. For example: "abcdcba", or "racecar".
*/
using System.Globalization;
var input = "aurora"; //racecar, aurora
int[] numArray = new int[input.Length];
List<int> numlist = new List<int>();
foreach (char c in input){
numlist.Add(c);
Console.WriteLine(c);
}
if(IsPalindrom(input)){
Console.WriteLine($"{input.ToUpper()} is a palindrom");
}
else{
Console.WriteLine($"{input.ToUpper()} is NOT a palindrom");
}
Console.WriteLine(IsPalindrom(input));
bool IsPalindrom(string s){
int left = 0;
int right = s.Length - 1;
while(left < right){
if(s[left] != s[right]){
return false;
}
left++;
right--;
}
return true;
}
/* Example 2: Given a sorted array of unique integers and a target integer, return true if there exists a pair of numbers that sum to target, false otherwise. This problem is similar to Two Sum. (In Two Sum, the input is not sorted).
For example, given nums = [1, 2, 4, 6, 8, 9, 14, 15] and target = 13, return true because 4 + 9 = 13.
*/
int[] numbers = [1, 2, 4, 6, 8, 9, 14, 15];
int targetNum = 13;
Console.WriteLine(CheckForTarget(numbers, targetNum));
bool CheckForTarget(int[] nums, int target){
int left = 0;
int right = nums.Length - 1;
while(left < right){
// current number is current sum
int curr = nums[left] + nums[right];
if(curr == target){
return true;
}
if(curr > target){
right--;
}
else{
left++;
}
}
return false;
}
// Example 3: Given two sorted integer arrays arr1 and arr2, return a new array that combines both of them and is also sorted.
int[] numOne = [1, 4, 3, 7, 20];
int[] numTwo = [3, 5, 6];
var newArray = CombineArrays(numOne, numTwo);
foreach(int num in newArray){
Console.WriteLine(num);
}
List<int> CombineArrays(int[] arr1, int[] arr2){
List<int> result = new List<int>();
int i = 0;
int j = 0;
while(i < arr1.Length && j < arr2.Length){
if(arr1[i] < arr2[j]){
result.Add(arr1[i]);
i++;
}
else{
result.Add(arr2[j]);
j++;
}
}
while (i < arr1.Length){
result.Add(arr1[i]);
i++;
}
while(j < arr2.Length){
result.Add(arr2[j]);
j++;
}
return result;
}
/*
Example 4: 392. Is Subsequence.
Given two strings s and t, return true if s is a subsequence of t, or false otherwise.
A subsequence of a string is a sequence of characters that can be obtained by deleting some (or none) of the characters from the original string, while maintaining the relative order of the remaining characters. For example, "ace" is a subsequence of "abcde" while "aec" is not.
*/
string stringOne = "ace";
string stringTwo = "abcde";
Console.WriteLine(IsSubsequence(stringOne, stringTwo));
bool IsSubsequence(string s, string t){
int i = 0;
int j = 0;
while(i < s.Length && j < t.Length){
if(s[i] == t[j]){
i++;
}
j++;
}
return i == s.Length;
}