× logo Home HTML CSS Javascript React-App Angular logo
logo

Mastering Top 10 Javascript Algorithms

**Algorithm Name**: Split the string

* **Questions**:
The task is how to split a string into new arrays containing strings, integers, and special characters, along with an explanation of time and space complexity: ( in JavaScript)

* **Time Complexity**:
- Brute Force: N/A
in this case since there is not much of a way to brute force this problem.

- Optimized:
The time complexity of this function is linear time O(n), where n is the length of the input string. This is because the code iterates through each character of the string once.

O(n), We must iterate through the input string, using regular expressions to identify and categorize each character. It then pushes each character into its respective array (strings, integers, or special characters).

* **Space Complexity**:
- Brute Force: N/A
- Optimized:
The space complexity is also linear time O(n) in the worst case. This occurs if the input string consists entirely of one type of character (e.g., all letters), causing one of the output arrays to grow linearly with the input size.

In more balanced cases, where the input has a mix of character types, the space used by each array will be less, but the overall space complexity remains O(n) because, in the worst-case scenario, the space usage grows linearly with the input size.

* **Approaches**:
- Optimized:
- First, .split('') is used to convert the string into an array where each character of the string is a separate element. using regular expressions to identify and categorize each character. It then pushes each character into its respective array (strings, integers, or special characters).

* **Sample Code**: javascript

*Questions*

Split the string, integer, and special characters into new arrays?
Make sure strings "4" and "5" are converted to integers and included in the integer array:

Follow-up questions for integer array
How to split it to 2 arrays with random elements and sum each array.

input = [ 'A', 'B','C', 1, 2, 3, '4', '5', 6, '@', '~', 'D' ]

Console Output:
integer = [1,2,3,4,5,6]
string = [ 'A', 'B', 'C', 'D' ]
chars = [ '@', '~' ]


Here's How to split strings, integers and special characters into new arrays in JavaScript, ensuring that strings "4" and "5" are converted to integers and included in the integer array:

- Optimized:

const input = [ 'A', 'B','C', 1, 2, 3, '4', '5', 6, '@', '~', 'D' ];
const integer = [];
const strings = [];
const chars = [];
let cNumber;

const separate = (arr) => {
const regex = /[A-Za-z]/;
for(c of arr) {
cNumber = parseInt(c, 10);
if(Number.isInteger(cNumber)) {
c = cNumber;
}
if(Number.isInteger(c)) {
integer.push(c);
} else if(regex.test(c)) {
strings.push(c);
} else {
chars.push(c);
}
}

console.log( "integer:", integer)
console.log( "strings:", strings)
console.log("chars", chars)
}

separate(input)

//Follow-up questions for integer array
//How to split it to 2 arrays with random elements and sum each array.

console.log("Here's How to split it to 2 arrays with random elements and sum each array.");

function splitAndSumRandomArrays(arr) {
const arrayLength = arr.length;
const array1 = [];
const array2 = [];

// Randomly distribute elements into the two arrays

for (let i = 0; i < arrayLength; i++) {
if (Math.random() < 0.5) {
array1.push(arr[i]);
} else {
array2.push(arr[i]);
}
}

// Calculate the sum of each array

const sum1 = array1.reduce((acc, num) => acc + num, 0);
const sum2 = array2.reduce((acc, num) => acc + num, 0);

//return the result

return {
array1,
sum1,
array2,
sum2,
};
}

//Verification Test Cases:
// Example usage: Array Test cases

const originalArray = [1,2,3,4,5,6]
const result = splitAndSumRandomArrays(originalArray);

console.log("Array 1:", result.array1);
console.log("Sum of Array 1:", result.sum1);
console.log("Array 2:", result.array2);
console.log("Sum of Array 2:", result.sum2);

Console Output:

Test case results

"Here's How to split strings, integers and special characters into new arrays in JavaScript."

"integer:" Array [1, 2, 3, 4, 5, 6]
"strings:" Array ["A", "B", "C", "D"]
"chars" Array ["@", "~"]

"Here's How to split it to 2 arrays with random elements and sum each array."

"Array 1:" Array [2, 4, 6]
"Sum of Array 1:" 12
"Array 2:" Array [1, 3, 5]
"Sum of Array 2:" 9



logo