Try to search your question here, if you can't find : Ask Any Question Now ?

Modifying index of a string

HomeCategory: stackoverflowModifying index of a string
craig asked 3 weeks ago

I am wondering about runtimes for replacing a character in a string (in javascript).

For example

let str = "hello world"; 
str[0] = "n"; 
console.log(str); // hello world

under strict mode, this throws an error because you cannot modify strings (read only).

How would you implement an O(1) time, O(1) space algorithm in js to do str[index] = char? In C, this is trivial because access is O(1) and you can modify that element without having to allocate a new array and copy values over.

I have found solutions using split to do this … but isnt this overkill? That means O(n) time and space.

Wondering about this mostly for interview questions since I use javascript

1 Answers
Best Answer
Mikhail answered 3 weeks ago
Your Answer

3 + 20 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com