-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.js
More file actions
244 lines (213 loc) · 6.82 KB
/
Copy pathpractice.js
File metadata and controls
244 lines (213 loc) · 6.82 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
238
239
240
241
242
243
244
//HOW DO YOU REVERSE A GIVEN STRING STRING IN PLACE
//VERIFY THE INPUTS/PROBLEM
//Ensure that input received is a string, and not a number or empty string.
//THINK ABOUT THE PROBLEM + VERBAL SOLUTION.
//Firstly i want to convert the string into an array of strings why so i can manipulate and access every substring of the array, after that i will reverse the string making the last the first and first the last... after reversing i will join the string back to a string.
//WRITE OUT THE FIRST VERSION.
function reverseString(str){
if(typeof str !=='string' || str === ''){
return null;
}
return str.split('').reverse().join('')
}
//VERIFY THE RESULTS
console.log(reverseString('Mercy'));
console.log(reverseString('Fatimah'));
console.log(reverseString('Ogbenjuwa'));
//DERIVE THE TIME COMPLEXITY
//BEST CASE =>('Mercy'));
//AVERAGE CASE =>('Fatimah'));
//WORST CASE =>('Ogbenjuwa'));
//BASED ON THE INPUT SIZE WHICH IS N, THAT IS DIRECTLY PROPORTIONAL TO THE INPUT SIZE. 0(n)=> LINEAR TIME COMPLEXITY.
//EXPLORE ALTERNATIVE ==> Please read properly.
function reverseStringInPlace(str) {
if (typeof str !== "string" || str === "") {
return null;
}
let strArr = str.split("");
let start = 0;
let end = strArr.length - 1;
while (start < end) {
let temp = strArr[start];
strArr[start] = strArr[end];
strArr[end] = temp;
start++;
end--;
}
return strArr.join("");
}
//2.HOW DO YOU PRINT DUPLICATE CHARACTERS FROM A STRING.
//VERIFY INPUTS/PROBLEMS
//Ensure that all incoming inputs, are of type of string, and are not empty. return null if criteria is not meet.
//THINK ABOUT THE PROBLEM + VERBAL SOLUTION
//My approach will be to count and store strings appearing more than once in a hash map.
//return duplicate string.
//WRITE OUT THE FIRST VERSION.
function duplicateCharacters (str){
if(typeof str !=='string' || str === ''){
return null;
}
let charCount = {};
let duplicateChar = '';
for(let char of str){
if(charCount[char]){
duplicateChar += char;
}else{
charCount[char] = 1;
}
}
return duplicateChar;
}
//VERIFY THE RESULTS
console.log(duplicateCharacters('qwertaswedrf'));
console.log(duplicateCharacters('mercy'));
console.log(duplicateCharacters('valentine'));
console.log(duplicateCharacters('file'));
//DERIVE THE TIME COMPLEXITY
//BEST CASE =>
//AVERAGE CASE =>
//WORST CASE =>
//EXPLORE ALTERNATIVES
//USING ARRAY METHODS
function duplicateCharacters(str){
if(typeof str !=='string' || str ===''){
return null
}
let charCount = new Set ();
let duplicateCount = [];
for(let char of str){
if(charCount.has(char)){
duplicateCount.push(char);
}else{
charCount.add(char);
}
}
return duplicateCount;
}
//3. HOW DO YOU CHECK IF TWO STRINGS ARE ANAGRAMS OF EACH OTHERS
//VERIFY THE INPUTS/PROBLEMS
//ENSURE BOTH INPUTS ARE STRINGS, AND NOT EMPTY.
//THINK ABOUT THE PROBLEM + VERBAL SOLUTION
//TO check if two strings are anagrams of each other, we need to iterate over them, check the frequency of each string and store in a hashmap. if the frequency are equal.
//WRITE OUT THE FIRST VERSION
function isAnagram (str1, str2){
if(typeof str1 !=='string' || str1 ==='' || typeof str2 !=='string' || str2 ===''){
return null;
}
let charCount1 = {};
let charCount2 = {};
for(let char of str1){
charCount1[char] =(charCount1[char] || 0) + 1;
}
for(let char of str2){
charCount2[char] =(charCount2[char] || 0) + 1;
}
for(let char in charCount1){ //= study
if(charCount1[char] !== charCount2[char]){
return false;
}
}
return true;
}
//VERIFY THE RESULTS
console.log(isAnagram('silent', 'listen'));
console.log(isAnagram('triangle', 'integral'));
console.log(isAnagram('file', 'life'));
console.log(isAnagram('hello', 'world'))
//DERIVE THE TIME COMPLEXITY.
//BEST CASE =>
//AVERAGE CASE =>
//WORST CASE =>
//NOTE O(N)
//EXPLORE ALTERNATIVES
function isAnagram(str1, str2){
if(typeof str1 !=='string' || str1 ==='' || typeof str2 !=='string' || str2 ===''){
return false;
}
let charcount1 = str1.split('').sort().join('');
let charcount2 = str2.split('').sort().join('');
return charcount1 === charcount2;
}
console.log(isAnagram('silent', 'listen'));
console.log(isAnagram('triangle', 'integral'));
console.log(isAnagram('file', 'life'));
console.log(isAnagram('hello', 'world'))
//O(log n)=> logarithmic time complexity.
//4.How do you count a number of vowels and consonants in a given string?
//VERIFY INPUTS/PROBLEMS
//INPUT MUST BE STRING RETURN NULL IF NOT STRING
//THINK ABOUT THE PROBLEM + VERBAL SOLUTION
//ITERATE OVER THE STRING, INCREMENT COUNT FOR VOWEL OR CONSONANT.
//WRITE OUT THE FIRST VERSION
function countVowelsAndConsonants(str) {
if (typeof str !== "string" || str === "") {
return null;
}
let vowels = "aeiouAEIOU";
let vowelsCount = 0;
let consonantsCount = 0;
for (let char of str) {
if (vowels.includes(char)) {
vowelsCount++;
} else if (char >= "a" && char <= "z" || char >= "A" && char <= "Z") {
consonantsCount++;
}
}
return{
vowels: vowelsCount,
consonants: consonantsCount
};
}
//VERIFY THE RESULTS
console.log(countVowelsAndConsonants('Mercy'));
console.log(countVowelsAndConsonants('Fatimah'));
console.log(countVowelsAndConsonants('Ogbenjuwa'));
//DERIVE THE TIME COMPLEXITY
//BEST CASE => Mercy
//AVERAGE CASE => Fatimah
//WORST CASE => Ogbenjuwa
//EXPLORE ALTERNATIVES
function countVowelsAndConsonants(str){
if(typeof str !=='string' || str ===''){
return null;
}
let vowels = str.match(/[aeiouAEIOU]/g) || []
let consonants = str.match(/[^aeiouAEIOU/s]/g) || [];
return {
vowel: vowels.length,
consonants: consonants.length
}
}
//O(n)
//HOW DO YOU COUNT THE OCCURENCE OF A GIVEN CHARCTER IN A STRING.
//VERIFY THE INPUTS/PROBLEMS
//ENSURE ALL INPUTS ARE STRINGS
//THINK ABOUT THE PROBLEM + VERBAL SOLUTION.
//CREATE TWO INPUTS, ONE FOR THE STRING YOU NEED TO CHECK, SECOND FOR THE SPECIAL CHARCATER.
//WRITE THE FIRST VERSION
function occurrence(str, char){
if (typeof str !=='string' || typeof char !='string' || str ==='' || char.length != 1){
return null;
}
let count = 0;
for(let c of str){
if(c === char){
count++;
}
}
return count
}
//VERIFY THE RESULTS
console.log(occurrence('Mercy', 'c'));
console.log(occurrence('Fatimah', 'a'))
console.log(occurrence('dsvcvgghgmzxxbbdqpiuggvb', 'x'))
//DERIVE THE TIME COMPLEXITY.
//=> 0(n)
//EXPLORE ALTERNATIVE
function occurence (str, char){
if (typeof str !=='string' || typeof char !='string' || str ==='' || char.length != 1){
return null;
}
return str.split('').filter(c => c === char).length
}
//O(n).