String data structure

This tutorial is a part of the Data Structures and Algorithms class:

String data structure:

  • is one of the most popular data structures.
  • is usually used to store text or any kind of information as text.
  • built-in in Javascript.

string data structure

⬆️ String data structure: on RAM and on code

How data looks like (print it out!)

lecture-3/string-data-structure.js
// initialize
let str = 'Today is a good day to learn Data Structures!'

// print out, see how data looks like
console.log(str) // Today is a good day to learn Data Structures!
$ node string-data-structure.js
Today is a good day to learn Data Structures!

How to insert/delete/access/search data

lecture-3/string-data-structure.js
// insert
str = str + ' So'
console.log(str) // Today is a good day to learn Data Structures! So
console.log(str.concat(' start learning!')) // Today is a good day to learn Data Structures! So start learning!

// access
console.log(str[29]) // D
console.log(str.substring(29, 44)); // Data Structures

// search
console.log(str.includes('good day')) // true
$ node string-data-structure.js
Today is a good day to learn Data Structures! So
Today is a good day to learn Data Structures! So start learning!
D
Data Structures
true

How fast to insert/delete/access/search data

Data Structure Insert Delete Access Search
String O(n): concat O(n): replace O(1) with index,
substring
O(n): includes

⬆️ Time complexity (worst case) per operation

Real life interview questions

  1. Do you know how your data looks like?
  2. What kind of information can string data structure store in real life applications?
  3. How do you insert/delete/access/search with string data structure?
  4. What is the Time Complexity of string data structure on insert/delete/access/search operation?
  5. Solve Leetcode #17: Letter Combinations of a Phone Number