🎒
Technical Interview Study Guide
  • 🍕Welcome!
  • 🍥Getting Started
    • Study Plan
    • Optimizing Revision
    • Summer 2024 Timeline
    • FAQs
  • 🥨Algorithms
    • Binary Search
    • Sorting
    • Recursion
    • Graph
    • Quick Select
    • Intervals
    • Binary
    • Geometry
    • Dynamic Programming
  • 🥞Data Structures
    • Arrays
      • Matrices
    • Strings
    • Linked Lists
      • Doubly Linked Lists
    • Hash Tables
    • Graphs
      • Trees
        • Binary Search Trees
        • Heaps
        • Tries
        • Segment Trees
    • Stacks
    • Queues
      • Double Ended Queues
    • Union-Find Disjoint Set (UFDS)
  • 🍡Problems Guide
    • Dynamic Programming Roadmap
      • Warmup
        • Climbing Stairs
        • Nth Tribonacci Number
        • Perfect Squares
      • Linear Sequence
        • Min Cost to Climb Stairs
        • Minimum Time to Make Rope Colorful
        • House Robber
        • Decode Ways
        • Minimum Cost for Tickets
        • Solving Questions with Brainpower
  • 🍣Other Technical Topics
    • General Problem Solving
    • Runtime Predictions
    • System Design
      • SQL
      • Accessing APIs
    • Operating Systems
  • 🍿Non-technical Topics
    • Behavioral Interviews
    • Resumes
Powered by GitBook
On this page
  • Runtime analysis
  • Time complexity
  • Space complexity
  • Corner cases
  • Techniques
  • Preprocessing list of words
  • Using DFS for pattern matching
  • Inverting what is stored
  • Augmenting nodes to store words that end at the node
  1. Data Structures
  2. Graphs
  3. Trees

Tries

Tries are special types of trees that make searching and storing strings more efficient

class Trie:
	def __init__(self):
		self.ch = [None] * 26
		self.end = False
	
	def insert(self, word):
		if not word:
			self.end = True
			return

		cur = self
		for ch in word:
			if not cur.ch[ord(ch) - ord('a')]:
				cur.ch[ord(ch) - ord('a')] = Trie()
			cur = cur.ch[ord(ch) - ord('a')]
		cur.end = True

	def word(self, target):
		if not target: return self.end
		
		cur = self
		for ch in target:
			if not cur.ch[ord(ch) - ord('a')]: return False
			cur = cur.ch[ord(ch) - ord('a')]
		return cur.end

Runtime analysis

Time complexity

m is the length of the string

Space complexity

Corner cases

  1. Searching for a string in an empty trie

  2. Inserting empty strings into a trie

Techniques

Preprocessing list of words

Use a trie to store a list of words to improve the efficiency for searching for a word of length k among n words

Using DFS for pattern matching

Pattern matching with values like * and . can be performed using DFS where branching is done to all valid "next" characters when these characters are encountered, otherwise normal traversal is used

Inverting what is stored

Some problems will use tries by storing a different set of data available, try out various inputs to see what works best

Augmenting nodes to store words that end at the node

This is a slight optimization to reduce the need to accumulate the strings as the Trie traversal is occurring

PreviousHeapsNextSegment Trees

Last updated 1 year ago

Search: O(m)O(m)O(m)

Insert: O(m)O(m)O(m)

Remove: O(m)O(m)O(m)

O(m×n)O(m \times n)O(m×n) where nnn is the number of strings and mmm is the length of the longest string

It takes only O(k)O(k)O(k) over O(n)O(n)O(n)

See problems like for reference

See problems like for reference

🥞
Word Search 2
Word Search 2