mirror of
https://github.com/prdlk/leetcode.git
synced 2026-09-16 23:16:26 +00:00
72 lines
2.2 KiB
Plaintext
72 lines
2.2 KiB
Plaintext
---
|
|
title: 'Arrays and Strings'
|
|
description: 'In algorithms, arrays and strings are very similar. They are both ordered collections of elements. The difference is that arrays are mutable, while strings are immutable.'
|
|
sidebar:
|
|
label: Arrays & Strings
|
|
---
|
|
|
|
|
|
Technically, an array can't be resized. A dynamic array, or list, can be. In the context of algorithm problems, usually when people talk about arrays, they are referring to dynamic arrays. In this entire course, we will be talking about dynamic arrays/lists, but we will just use the word "array".
|
|
|
|
## Time Complexity
|
|
|
|
Arrays and strings are both ordered collections of elements. The difference is that arrays are mutable, while strings are immutable.
|
|
|
|
| Operation | Array/List | String(Immutable) |
|
|
| --------- | ---------- | ----------------- |
|
|
| Appending to end | *O(1) | O(n) |
|
|
| Popping from end | O(1) | O(n) |
|
|
| Insertion, not from end | O(n) | O(n) |
|
|
| Deletion, not from end | O(n) | O(n) |
|
|
| Modifying an element | O(1) | O(n) |
|
|
| Random access | *O(1) | O(n) |
|
|
| Checking if an element exists | O(1) | O(n) |
|
|
|
|
:::note[Clarification 1]
|
|
Appending to the end of a list is amortized O(1) time complexity. This means that the time complexity of appending to the end of a list is the same as the time complexity of appending to the end of an array.
|
|
:::
|
|
|
|
:::note[Clarification 2]
|
|
Random access in this context means that you can access an element at any index in constant time.
|
|
:::
|
|
|
|
## Patterns
|
|
|
|
<Columns cols={2}>
|
|
<Column>
|
|
<Tile
|
|
title="Two Pointers"
|
|
description="Ship your first page in minutes."
|
|
href="/arrays-and-strings/two-pointers"
|
|
>
|
|
<Icon icon="rocket" size={28} />
|
|
</Tile>
|
|
</Column>
|
|
<Column>
|
|
<Tile
|
|
title="Sliding Window"
|
|
description="Ship your first page in minutes."
|
|
href="/docs/quickstart"
|
|
>
|
|
<Icon icon="rocket" size={28} />
|
|
</Tile>
|
|
</Column>
|
|
<Column>
|
|
<Tile
|
|
title="Prefix Sum"
|
|
description="Ship your first page in minutes."
|
|
href="/docs/quickstart"
|
|
>
|
|
<Icon icon="rocket" size={28} />
|
|
</Tile>
|
|
</Column>
|
|
</Columns>
|
|
|
|
|
|
|
|
## Study deck
|
|
|
|
Drill the essentials — click the deck, then use the arrow keys.
|
|
|
|
<StudyDeck deck="arrays-and-strings" />
|