-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCodeString.cs
More file actions
237 lines (208 loc) · 6.7 KB
/
Copy pathLeetCodeString.cs
File metadata and controls
237 lines (208 loc) · 6.7 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AlgorithmPractice
{
public class LeetCodeString
{
/*Remove All Adjacent Duplicates In String*/
public string RemoveDuplicates(string s)
{
for (int i = 0; i < s.Length - 1;)
{
if (s[i] == s[i + 1])
{
s = s.Remove(i, 2);
if (i != 0)
{
i = i - 1;
}
}
else
{
i++;
}
}
return s;
}
//Length of Last Word
public int LengthOfLastWord(string s)
{
if (string.IsNullOrEmpty(s))
return 0;
int length = 0;
bool begin = false;
for (int i = s.Length - 1; i >= 0; i--)
{
if (s[i] == ' ' && begin == false)
{
continue;
}
else if (s[i] == ' ' && begin == true)
{
return length;
}
else
{
if (begin == false)
begin = true;
length += 1;
}
}
return length;
}
//Multiply Strings
public string Multiply(string num1, string num2)
{
if (num2 == "0" || num1 == "0")
return "0";
if (num1 == "1")
return num2;
if (num2 == "1")
return num1;
string[] subResult = new string[num2.Length];
int index = 0;
// get all the subResult string
for (int i = num2.Length - 1; i >= 0; i--)
{
string sub = "";
//how many 0 should be
for (int j = 0; j < index; j++)
{
sub = sub + "0";
}
int extra = 0;
for (int k = num1.Length - 1; k >= 0; k--)
{
int subMutil = (num1[k] - '0') * (num2[i] - '0') + extra;
int currentNumber = subMutil % 10;
sub = sub + currentNumber;
extra = subMutil / 10;
}
if (extra > 0)
{
sub = sub + extra;
}
subResult[index] = sub;
index++;
}
string finalResult = subResult[0];
//get the sum result but from 0 to higher
for (int i = 1; i < subResult.Length; i++)
{
int extra = 0;
int length = subResult[i].Length <= finalResult.Length ? subResult[i].Length : finalResult.Length;
for (int j = 0; j < finalResult.Length; j++)
{
if (j == subResult[i].Length)
{
int number = finalResult[j] - '0' + extra;
int currentNumber = number % 10;
extra = number / 10;
finalResult = updateString(currentNumber, j, finalResult);
break;
}
else
{
int number = finalResult[j] - '0' + (subResult[i][j] - '0') + extra;
int currentNumber = number % 10;
extra = number / 10;
finalResult = updateString(currentNumber, j, finalResult);
}
}
if(subResult[i].Length> length)
{
for (int k = length; k < subResult[i].Length; k++)
{
int number = subResult[i][k] - '0' + extra;
int currentNumber = number % 10;
extra = number / 10;
finalResult=finalResult+currentNumber;
}
}
if (extra > 0)
{
finalResult = finalResult + extra;
}
}
// converse the order
string final = "";
for (int i = finalResult.Length - 1; i >= 0; i--)
{
final += finalResult[i];
}
return final;
}
private string updateString(int num, int index, string original)
{
char[] temp = original.ToCharArray();
temp[index] = char.Parse(num.ToString());
return new string(temp);
}
public bool CanConstruct(string ransomNote, string magazine)
{
Dictionary<char, int> letters = new Dictionary<char, int>();
for (int i = 0; i < magazine.Length; i++)
{
if (letters.ContainsKey(magazine[i]))
{
letters[magazine[i]] += 1;
}
else
{
letters.Add(magazine[i], 1);
}
}
for (int i = 0; i < ransomNote.Length; i++)
{
if (letters.ContainsKey(ransomNote[i]))
{
letters[ransomNote[i]] -= 1;
if (letters[ransomNote[i]] <= 0)
letters.Remove(ransomNote[i]);
}
else
{
return false;
}
}
return true;
}
public int GetFirstNodeDuplicate(string s)
{
Dictionary<char, int> hash = new Dictionary<char, int>();
for(int i=0;i<s.Length;i++)
{
if(hash.Keys.Contains(s[i]))
{
int number = hash[s[i]];
hash[s[i]] = number + 1;
}
else
{
hash.Add(s[i], 1);
}
}
for(int i=0;i<s.Length;i++)
{
if (hash[s[i]] == 1)
return i;
}
return -1;
}
//get the length of the smallest repeat string
public int GetRepeat(string s)
{
for (int i = 1; i < (s.Length / 2 + 1); i++)
{
string subString = s.Substring(0, i);
string temp = s.Replace(subString, "");
if (temp.Length == 0)
return subString.Length;
}
return s.Length;
}
}
}