54 lines
2.1 KiB
TypeScript
54 lines
2.1 KiB
TypeScript
export function xorMergeStrings(strings: string[]) {
|
|
// Split the input strings into arrays of lines
|
|
const [str1, str2] = strings;
|
|
const lines1 = str1.split("\n");
|
|
console.log("🚀 ~ xorMergeStrings ~ lines1:", lines1);
|
|
const lines2 = str2.split("\n");
|
|
console.log("🚀 ~ xorMergeStrings ~ lines2:", lines2);
|
|
|
|
// Determine the maximum number of lines
|
|
const maxLines = Math.max(lines1.length, lines2.length);
|
|
|
|
// Initialize an array to hold the merged lines
|
|
const mergedLines = [];
|
|
|
|
// Iterate through each line index up to the maximum number of lines
|
|
for (let i = 0; i < maxLines; i++) {
|
|
// Get the current line from each string, or an empty string if the line does not exist
|
|
const maxLength = Math.max(lines1[i].length, lines2[i].length);
|
|
const line1 = lines1[i].padEnd(maxLength, " ");
|
|
console.log("🚀 ~ xorMergeStrings ~ line1: \n", line1);
|
|
const line2 = lines2[i].padEnd(maxLength, " ");
|
|
console.log("🚀 ~ xorMergeStrings ~ line2: \n", line2);
|
|
|
|
// Determine the maximum length of the current lines
|
|
console.log("🚀 ~ xorMergeStrings ~ maxLength:", maxLength);
|
|
|
|
// Initialize an array to hold the merged characters for the current line
|
|
const mergedLine = [];
|
|
|
|
// Iterate through each character index up to the maximum length
|
|
for (let j = 0; j < maxLength; j++) {
|
|
// Get the current character from each line, or a space if the character does not exist
|
|
const char1 = line1[j];
|
|
const char2 = line2[j];
|
|
|
|
// Determine if each character is a whitespace character
|
|
const isWhitespace1 = char1.trim() === "";
|
|
const isWhitespace2 = char2.trim() === "";
|
|
|
|
// XOR merge the characters based on the whitespace criteria
|
|
const mergedChar = isWhitespace1 ? char2 : char1;
|
|
|
|
// Add the merged character to the merged line
|
|
mergedLine.push(mergedChar);
|
|
}
|
|
|
|
// Join the merged line array into a string and add it to the merged lines array
|
|
mergedLines.push(mergedLine.join(""));
|
|
}
|
|
|
|
// Join the merged lines array into a single string with newline characters
|
|
return mergedLines.join("\n");
|
|
}
|