/*
 * Copyright (C) 2015 Imran Mammadli
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package hash.strings.closestPair;

public class Solution1 
{
	/**
	 * Find the closest repeated pair of words in an array
	 * 
	 * @param words the array of words
	 * @return the distance between a closest pair of equal entries
	 * 
	 * @time <i>O(n<sup>2</sup>)</i>
	 * @space <i>O(1)</i>
	 **/
	public static int getClosestPair(String[] words)
	{
		int distance = Integer.MAX_VALUE;
				
		for (int i = 0; i < words.length; i++)
		{
			for (int j = i + 1; j < words.length; j++)
			{
				if (words[i].equals(words[j]))
				{
					distance = Math.min(distance, j - i);
				}
			}
		}
		
		return distance;
	}
}