What is the space complexity of this string manipulation code?

大城市里の小女人 提交于 2019-12-05 08:13:13

it's a bit more complicated, i think:

the max number of iterations before some character will be encountered twice is the size of the alphabet the string is built over.

if this size is finite, time complexity is O(1), if it's not, the boolean array cannot be of finite size, thus, space complexity will be O(n).

Yamil Marques de Mello

The space complexity in that example is O(N) because the string is received as parameter; we don't know exactly it's size, and taking into account that the space complexity advices about the memory consumption in time of the algorithm, it will vary depending on the size of "str". Because of that N should be used.

Exactly the same happens if I have for example:

public void someMethod(int a[], char s, int w){...}

It will be O(N) because of "a[]" (we don't know it's size).

On the other hand:

public void someMethod(char s, int a, int x){...}

It will be O(1) (constant). Due we already know the memory allocated for the necessary attributes.

Hope it helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!