-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2024-01-07 Codewars 7 Kyu Fundamentals - Maximum Length Difference.js
148 lines (101 loc) · 3.72 KB
/
2024-01-07 Codewars 7 Kyu Fundamentals - Maximum Length Difference.js
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
// 1/7/24 Sunday Codewars 7 Kyu Fundamentals - Maximum Length Difference
// https://www.codewars.com/kata/5663f5305102699bad000056/train/javascript
/*
You are given two arrays a1 and a2 of strings. Each string is composed with letters from a to z. Let x be any string in the first array and y be any string in the second array.
Find max(abs(length(x) − length(y)))
If a1 and/or a2 are empty return -1 in each language except in Haskell (F#) where you will return Nothing (None).
Example:
a1 = ["hoqq", "bbllkw", "oox", "ejjuyyy", "plmiis", "xxxzgpsssa", "xxwwkktt", "znnnnfqknaz", "qqquuhii", "dvvvwz"]
a2 = ["cccooommaaqqoxii", "gggqaffhhh", "tttoowwwmmww"]
mxdiflg(a1, a2) --> 13
Bash note:
input : 2 strings with substrings separated by ,
output: number as a string
*/
// 2nd attempt - works
// ****** Using array - works
function mxdiflg(a1, a2) {
if(a1.length === 0 || a2.length === 0) {
return -1;
}
let a1MaxLength = Math.max(...a1.map(str => str.length));
let a1MinLength = Math.min(...a1.map(str => str.length));
let a2MaxLength = Math.max(...a2.map(str => str.length));
let a2MinLength = Math.min(...a2.map(str => str.length));
return Math.max(Math.abs(a1MaxLength - a2MinLength), Math.abs(a1MinLength - a2MaxLength));
}
// First run of 2nd attempt, trying new approach using objects
function mxdiflg(a1, a2) {
if(a1.length === 0 || a2.length === 0) {
return -1
}
let a1Longest = 0;
let a1Shortest = 99999;
let a2Longest = 0;
let a2Shortest = 99999;
for(let i = 0; i < a1.length; i++) {
if(a1[i].length > a1Longest) {
a1Longest = a1[i].length;
}
}
for(let j = 0; j < a2.length; j++) {
if(a2[j].length > a2Longest) {
a2Longest = a2[j].length;
}
}
return Math.abs(a1Longest - a2Longest);
}
// Using object - skipping b/c it's more than necessary; can just put the lengths of the strings in an array
function mxdiflg(a1, a2) {
if(a1.length === 0 || a2.length === 0) {
return -1;
}
let stringLengths = {};
for(let i = 0; i < a1.length; i++) {
if(a1[i].length in stringLengths) {
stringLengths[a1[i].length] += 1;
} else {
stringLengths[a1[i].length] = 1;
}
}
for(let j = 0; j < a2.length; j++) {
if(a2[j].length in stringLengths) {
stringLengths[a2[j].length] += 1;
} else {
stringLengths[a2[j].length] = 1;
}
}
}
// ****** Using array - works, copied to top
function mxdiflg(a1, a2) {
if(a1.length === 0 || a2.length === 0) {
return -1;
}
let a1MaxLength = Math.max(...a1.map(str => str.length));
let a1MinLength = Math.min(...a1.map(str => str.length));
let a2MaxLength = Math.max(...a2.map(str => str.length));
let a2MinLength = Math.min(...a2.map(str => str.length));
return Math.max(Math.abs(a1MaxLength - a2MinLength), Math.abs(a1MinLength - a2MaxLength));
}
// 1st attempt, not working - prompt is a bit confusing, should be subtracting the highest max from the lowest min of either array
function mxdiflg(a1, a2) {
if(a1.length === 0 || a2.length === 0) {
return -1
}
let a1Longest = 0;
let a2Longest = 0;
for(let i = 0; i < a1.length; i++) {
if(a1[i].length > a1Longest) {
a1Longest = a1[i].length;
}
}
for(let j = 0; j < a2.length; j++) {
if(a2[j].length > a2Longest) {
a2Longest = a2[j].length;
}
}
return Math.abs(a1Longest - a2Longest);
}
/* =============
Other Solutions
============= */