-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0555-split-concatenated-strings.js
More file actions
49 lines (43 loc) · 1.48 KB
/
0555-split-concatenated-strings.js
File metadata and controls
49 lines (43 loc) · 1.48 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
/**
* Split Concatenated Strings
* Time Complexity: O(L^2)
* Space Complexity: O(L)
*/
var splitLoopedString = function (strs) {
const processedStrings = strs.map((inputString) => {
const reversedSegment = inputString.split("").reverse().join("");
return inputString > reversedSegment ? inputString : reversedSegment;
});
let longestOverallString = "";
const numSegments = processedStrings.length;
for (let segmentIndex = 0; segmentIndex < numSegments; segmentIndex++) {
const originalSegment = strs[segmentIndex];
const reversedOriginalSegment = originalSegment
.split("")
.reverse()
.join("");
const segmentsAfterCurrent = processedStrings.slice(segmentIndex + 1);
const segmentsBeforeCurrent = processedStrings.slice(0, segmentIndex);
const middleConcatenation =
segmentsAfterCurrent.join("") + segmentsBeforeCurrent.join("");
for (const segmentOrientation of [
originalSegment,
reversedOriginalSegment,
]) {
for (
let cutPoint = 0;
cutPoint <= segmentOrientation.length;
cutPoint++
) {
const leadingPart = segmentOrientation.substring(cutPoint);
const trailingPart = segmentOrientation.substring(0, cutPoint);
const currentCandidate =
leadingPart + middleConcatenation + trailingPart;
if (currentCandidate > longestOverallString) {
longestOverallString = currentCandidate;
}
}
}
}
return longestOverallString;
};