You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
142 lines
6.7 KiB
142 lines
6.7 KiB
5 years ago
|
---
|
||
|
id: string
|
||
3 years ago
|
title: String cheatsheet for coding interviews
|
||
|
description: String study guide for coding interviews, including practice questions, techniques, time complexity, and recommended resources
|
||
|
keywords:
|
||
|
[
|
||
|
string coding interview study guide,
|
||
|
string tips for coding interviews,
|
||
|
string practice questions,
|
||
|
string useful techniques,
|
||
|
string time complexity,
|
||
|
string recommended study resources,
|
||
|
]
|
||
|
sidebar_label: String
|
||
3 years ago
|
toc_max_heading_level: 2
|
||
5 years ago
|
---
|
||
7 years ago
|
|
||
3 years ago
|
<head>
|
||
|
<meta property="og:image" content="https://www.techinterviewhandbook.org/social/algorithms/algorithms/algorithms-string.png" />
|
||
|
</head>
|
||
|
|
||
3 years ago
|
## Introduction
|
||
5 years ago
|
|
||
3 years ago
|
A string is a sequence of characters. Many tips that apply to arrays also apply to strings. You're recommended to read the page on [Arrays](./array.md) before reading this page.
|
||
5 years ago
|
|
||
3 years ago
|
Common data structures for looking up strings:
|
||
5 years ago
|
|
||
|
- [Trie/Prefix Tree](https://en.wikipedia.org/wiki/Trie)
|
||
|
- [Suffix Tree](https://en.wikipedia.org/wiki/Suffix_tree)
|
||
|
|
||
3 years ago
|
Common string algorithms:
|
||
5 years ago
|
|
||
|
- [Rabin Karp](https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm) for efficient searching of substring using a rolling hash
|
||
|
- [KMP](https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm) for efficient searching of substring
|
||
|
|
||
3 years ago
|
## Time complexity
|
||
5 years ago
|
|
||
3 years ago
|
A strings is an array of characters, so the time complexities of basic string operations will closely resemble that of array operations.
|
||
5 years ago
|
|
||
3 years ago
|
| Operation | Big-O |
|
||
|
| --------- | ----- |
|
||
|
| Access | O(1) |
|
||
|
| Search | O(n) |
|
||
|
| Insert | O(n) |
|
||
|
| Remove | O(n) |
|
||
5 years ago
|
|
||
3 years ago
|
### Operations involving another string
|
||
5 years ago
|
|
||
3 years ago
|
Here we assume the other string is of length m.
|
||
|
|
||
|
| Operation | Big-O | Note |
|
||
|
| --- | --- | --- |
|
||
|
| Find substring | O(n.m) | This is the most naive case. There are more efficient algorithms for string searching such as the [KMP algorithm](https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm) |
|
||
|
| Concatenating strings | O(n + m) | |
|
||
|
| Slice | O(m) | |
|
||
2 years ago
|
| Split (by token) | O(n + m) | |
|
||
3 years ago
|
| Strip (remove leading and trailing whitespaces) | O(n) | |
|
||
|
|
||
3 years ago
|
## Things to look out for during interviews
|
||
|
|
||
|
Ask about input character set and case sensitivity. Usually the characters are limited to lowercase Latin characters, for example a to z.
|
||
|
|
||
3 years ago
|
## Corner cases
|
||
5 years ago
|
|
||
3 years ago
|
- Empty string
|
||
|
- String with 1 or 2 characters
|
||
|
- String with repeated characters
|
||
|
- Strings with only distinct characters
|
||
5 years ago
|
|
||
3 years ago
|
## Techniques
|
||
5 years ago
|
|
||
|
Many string questions fall into one of these buckets.
|
||
|
|
||
3 years ago
|
### Counting characters
|
||
|
|
||
|
Often you will need to count the frequency of characters in a string. The most common way of doing that is by using a hash table/map in your language of choice. If your language has a built-in Counter class like Python, ask if you can use that instead.
|
||
|
|
||
|
If you need to keep a counter of characters, a common mistake is to say that the space complexity required for the counter is O(n). The space required for a counter of a string of latin characters is O(1) not O(n). This is because the upper bound is the range of characters, which is usually a fixed constant of 26. The input set is just lowercase Latin characters.
|
||
5 years ago
|
|
||
3 years ago
|
#### String of unique characters
|
||
|
|
||
|
A neat trick to count the characters in a string of unique characters is to use a 26-bit bitmask to indicate which lower case latin characters are inside the string.
|
||
5 years ago
|
|
||
|
```py
|
||
|
mask = 0
|
||
3 years ago
|
for c in word:
|
||
5 years ago
|
mask |= (1 << (ord(c) - ord('a')))
|
||
|
```
|
||
|
|
||
3 years ago
|
To determine if two strings have common characters, perform `&` on the two bitmasks. If the result is non-zero, ie. `mask_a & mask_b > 0`, then the two strings have common characters.
|
||
5 years ago
|
|
||
|
### Anagram
|
||
|
|
||
3 years ago
|
An anagram is word switch or word play. It is the result of rearranging the letters of a word or phrase to produce a new word or phrase, while using all the original letters only once. In interviews, usually we are only bothered with words without spaces in them.
|
||
5 years ago
|
|
||
3 years ago
|
To determine if two strings are anagrams, there are a few approaches:
|
||
5 years ago
|
|
||
3 years ago
|
- Sorting both strings should produce the same resulting string. This takes O(n.log(n)) time and O(log(n)) space.
|
||
|
- If we map each character to a prime number and we multiply each mapped number together, anagrams should have the same multiple (prime factor decomposition). This takes O(n) time and O(1) space. Examples: [Group Anagram](https://leetcode.com/problems/group-anagrams/)
|
||
5 years ago
|
- Frequency counting of characters will help to determine if two strings are anagrams. This also takes O(n) time and O(1) space.
|
||
|
|
||
|
### Palindrome
|
||
|
|
||
3 years ago
|
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as `madam` or `racecar`.
|
||
5 years ago
|
|
||
|
Here are ways to determine if a string is a palindrome:
|
||
|
|
||
|
- Reverse the string and it should be equal to itself.
|
||
3 years ago
|
- Have two pointers at the start and end of the string. Move the pointers inward till they meet. At every point in time, the characters at both pointers should match.
|
||
5 years ago
|
|
||
3 years ago
|
The order of characters within the string matters, so hash tables are usually not helpful.
|
||
5 years ago
|
|
||
3 years ago
|
When a question is about counting the number of palindromes, a common trick is to have two pointers that move outward, away from the middle. Note that palindromes can be even or odd length. For each middle pivot position, you need to check it twice - once that includes the character and once without the character. This technique is used in [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/).
|
||
5 years ago
|
|
||
3 years ago
|
- For substrings, you can terminate early once there is no match
|
||
|
- For subsequences, use dynamic programming as there are overlapping subproblems. Check out [this question](https://leetcode.com/problems/longest-palindromic-subsequence/)
|
||
5 years ago
|
|
||
3 years ago
|
## Essential questions
|
||
|
|
||
|
_These are essential questions to practice if you're studying for this topic._
|
||
5 years ago
|
|
||
3 years ago
|
- [Valid Anagram](https://leetcode.com/problems/valid-anagram)
|
||
|
- [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)
|
||
5 years ago
|
- [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)
|
||
3 years ago
|
|
||
|
## Recommended practice questions
|
||
|
|
||
|
_These are recommended questions to practice after you have studied for the topic and have practiced the essential questions._
|
||
|
|
||
5 years ago
|
- [Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/)
|
||
3 years ago
|
- [Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string)
|
||
5 years ago
|
- [Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/description/)
|
||
|
- [Group Anagrams](https://leetcode.com/problems/group-anagrams/)
|
||
|
- [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/)
|
||
5 years ago
|
- [Encode and Decode Strings (LeetCode Premium)](https://leetcode.com/problems/encode-and-decode-strings/)
|
||
3 years ago
|
|
||
|
## Recommended courses
|
||
|
|
||
|
import AlgorithmCourses from '../\_courses/AlgorithmCourses.md'
|
||
|
|
||
|
<AlgorithmCourses />
|