# Tag Info

### How does editing software (like Microsoft word or Gmail) pick the 2nd string to compare in Levenshtein distance?

Yes, the entire dictionary is compared against each word. This can be fast by using a trie and an algorithm similar to Levenshtein's. I have built my own spelling corrector that checks words against a ...

### How does editing software (like Microsoft word or Gmail) pick the 2nd string to compare in Levenshtein distance?

Companies with search engines (e.g. Microsoft or Google) don't always directly search for the string with the smallest Levenshtein distance. They have a huge database of search queries, from which ...
Accepted

### Longest common substring in linear time

Let $m$ and $n$ be the lengths of two given strings, Linear time assuming the size of the alphabet is constant. Yes, the longest common substring of two given strings can be found in $O(m+n)$ time, ...

### How to check if two strings are permutations of each other using O(1) additional space?

Denote the arrays by $A,B$, and suppose they are of length $n$. Suppose first that the values in each array are distinct. Here is an algorithm that uses $O(1)$ space: Compute the minimum values of ...
Accepted

### Assigning a unique representation to equivalent circular queues

Enumerate all possible rotations of the queue. Take the lexicographically first of them. Use this as your representative. If you want a short index into a hash table, take the hash of that. Then ...
Accepted

### From Guido's essays, how does this function avoid quadratic behavior in a string concatenation algorithm?

Let's assume that adding two strings of lengths $a,b$ takes time $a+b$. Consider the following strategy to convert a list of $n$ characters into a list: Read the list in chunks of $k$, convert them ...
Accepted

### Count number of non-contiguous occurrences in string

A dynamic programming algorithm in $\mathcal{O}(|S| |T|)$ should do the trick. Let's denote $S = s_1…s_m$ and $T = t_1…t_n$. For $0\leqslant i \leqslant m$, $0\leqslant j \leqslant n$, let $N(i, j)$ ...
Accepted

### Can we prove that any set of finite strings without substrings is finite?

Assuming that by substring you mean consecutive substring, take $$L= \{10^n1 : n \ge 0\}.$$ On the other hand, if you refer to nonconsecutive substrings, or subsequence ordering, then Higman's Lemma ...
Accepted

### Find all substrings that fit the mask with asterisks

Yes, there is a more efficient algorithm. Your algorithm can take exponential time. You can check whether there exists any match in $O(nm)$ time, where $n$ is the length of text and $m$ is the ...

### How does editing software (like Microsoft word or Gmail) pick the 2nd string to compare in Levenshtein distance?

I just tried and found that the spelling checker on my phone finds a perfectly fine replacement for “gekki wirkd”. Look at your keyboard, and it is obvious. A good spelling checker does much better ...
Accepted

### What is the name of the following binary encoding?

Your encoding is not self-terminating, which makes it somewhat less useful than encodings such as universal codes. Given an integer $n \geq 0$, write $n+2$ in binary without leading zeroes, and remove ...

### Have I invented a new data structure?

I've never seen this data structure before. However, it doesn't seem like a good choice for storing a set of words, for most purposes. I see three significant disadvantages: Performance. Looking up ...
Accepted

### How to check if two strings are permutations of each other using O(1) additional space?

The naive approach would be building histograms of both strings and checking whether they are the same. Since we are not allowed to store such a data structure (whose size would be linear to the size ...
Accepted

### Finding the smallest string that contains a given set of substrings

This problem is called shortest superstring problem. John Gallant, David Maier and James Astorer proved it is NP-hard in 19791. Given two strings $A$ and $B$, let $|A|$ denote the length of $A$, and ...

### Find all n bit numbers with k ones and unique under circular shift

Binary strings considered up to rotation are known as necklaces. You are interested in enumerating binary necklaces with given density. You can find one solution in Wang and Savage, A Gray Code for ...
Accepted

### Find all pairs of strings in a set with Levenshtein distance < d

There's a "trick" you can use that might potentially speed up your algorithm a little: shingling. No guarantees that it'll necessarily help in your particular case, though. Lemma. If the edit ...

### How to speed up process of finding duplicates/similar items in a large amount of strings?

Edit distance is definitely not the way to proceed. The standard approach toward near-identical document deduplication is to compute hashes of shingles. Here's one way to do it: Compute a set of k-...

Accepted

### What do you call a function from symbols of alphabet to languages?

It looks like it refers to a substitution, but that the given definition is incomplete. Anyway, in formal language theory, people often use monoid morphisms between free monoids. To avoid overloading ...
Accepted

### Space complexity of string indices: O(1) or O(log|S|)?

These are correct (unless you explicitly specify a non-standard model of computing): $O(1)$ space, $O(1)$ words of space, $O(\log|S|)$ bits of space.

### String matching algorithm - check if a string matches a pattern

There are various algorithms for pattern matching in string. Exact String matching algorithms Brute Force Rabin Karp Boyer-Moree KMP Aho Corasick etc. Approximate String Matching Algorithm ...
Accepted

### Counting substrings with a given number of different characters in O(N)

You can solve this in $O(n)$ time using two (well, three) pointers that both move leftward. Let $S$ be the string. We'll let $i$ range from $n$ down to $1$, and for each value of $i$, we're going to ...
Accepted

### Complexity of Block edit distance with Swapping only

When one operation is exactly "removing a block and inserting it between two other positions", the problem of computing the string distance is known as Transposition Distance. It is NP-hard even if ...
Accepted

### Algorithm: given very large file of strings, find lines containing substring

You can construct a suffix tree for that very large file and once generated the same can be used for querying. For Suffix tree generation use Suffix Array approach, there are many algorithm to ...
Here is a general solution for an alphabet of size $d \geq 3$ and a string of length $n$. Every string of length $n$ has $n-\ell+1$ substrings of length $\ell$. Hence the number of different ...