We present an efficient dynamic programming algorithm to determine the optimal assortment and inventory levels in a single-period problem with stockout-based substitution. In our model, total customer demand is random and comprises fixed proportion of customers of different types. Customer preferences are modeled through the definition of these types. Each customer type corresponds to a specific preference ordering among products. A customer purchases the highest-ranked product, according to his type (if any), that is available at the time of his visit to the store (stockout-based substitution). We solve the optimal assortment problem using a dynamic programming formulation. We establish structural properties of the value function of the dynamic program that, in particular, help to characterize multiple local maxima. We use the properties of the optima to solve the problem in pseudopolynomial time. Our algorithm also gives a heuristic for the general case, i.e., when the proportion of customers of each type is random. In numerical tests, this heuristic performs better and faster than previously known methods, especially when the mean demand is large, the degree of substitutability is high, the population is homogeneous, or prices and/or costs vary across products.