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
Avatarcraig asked 5 months 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
AvatarMikhail answered 5 months ago
Your Answer

6 + 16 =

Popular Tags

WP Facebook Auto Publish Powered By :