A title for a movie can be ambiguous. (Eg. The Lord of the Rings, Lord of the Rings, Lord of the Rings, The) There exists a database entry that has a list of movie titles mapped to a unique identifier.

I am trying to write a method that takes a String title input and returns the unique identifier mapped to it after having resolved any ambiguity.(Is is a sequel? A remake?)

What is a good data structure to be used here?

2 Answers 2


Your question is a little unclear as the title asks for an algorithm, but you also ask for a data structure. I will describe what I did with a similar issue, dealing with music.

The way I approached it was with a series of string transformations that produced a string with as much ambiguity removed as possible.

A few of the rules:

  • Remove all whitespace
  • Change all non-ASCII letters to ASCII equivalents. (ö -> o)
  • Change everything to uppercase
  • If a comma is encountered, swap the right side of the common with the left
  • Remove common words like "the", "of", etc.
  • Change Roman numerals to Arabic ("VII" -> 7)

So I'd end up with:

Blue Öyster Cult -> BLUEOYSTERCULT
Amos, Tori -> TORIAMOS
The Red Hot Chili Peppers -> REDHOTCHILIPEPPERS

I then used that for all comparisons, though it was never exposed to the user. In my case, I just used this as the identifier.

The rules were necessarily a bunch of heuristics developed by experimenting with real CDDB data. It obviously wasn't guaranteed to be foolproof, but it wasn't that hard to find a set that worked most of the time.

Your issue isn't quite the same. Remakes will be a problem because your titles will match. That might be partially solvable by looking for dates in the title ("Total Recall (2013)") but I suspect that data will often be missing.

  • These suggestions are good for English titles (and remind me of slug algorithms), but are difficult to pull off in a general setting. For example, most characters do not have “ASCII counterparts”, and other languages have different stopwords. Also, collisions could occur, and it is crucial to let a user override the automatic categorization. The normalized string can be used to map the item to an ID, but the string shouldn't be the ID itself.
    – amon
    Mar 9, 2015 at 18:54
  • Yeah, my case was English only, though with movies and music, you can often get away with that.
    – user53141
    Mar 9, 2015 at 19:53
  • That leads to "The The" => "" :-) Ok, an exception.
    – ott--
    Mar 13, 2015 at 20:10
  • Hah! I had an exception rule for that band!
    – user53141
    Mar 13, 2015 at 21:20

I would begin by sanitizing the input data much like @Steven Burnap and then calculate the Levenshtein distance between the input and all known movie titles. Return the top couple results. Since there's likely to be a high degree of variation between your users' input and your list of known movie titles, an exact string search algorithm isn't a good fit.

You'll have to experiment to find the most accurate and quickest fuzzy search algorithm for your use case, and you'll have to decide if it's faster to perform the calculation in the DB or on an app server. It just comes down to your app architecture.

edit: When you're dealing with sequels, I would treat all titles within a series as having an equal weight, and then choose the most appropriate answer based upon some external factor. For your software's initial release, maybe just sort by movie release date. For a second release, poll an external API to find the most buzzworthy title. For a third release, perhaps gather some user metrics to find the most relevant result (for instance, if the user regularly prefers sequels over originals, give the sequel a higher score).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.