Skip to content

Commit acf1afb

Browse files
solves shortest completing word
1 parent e15ed9f commit acf1afb

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# LeetCode Algorithms
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-182/2081-1f425f.svg)
4-
![problems-solved-java](https://img.shields.io/badge/Java-182/2081-1abc9c.svg)
5-
![problems-solved-python](https://img.shields.io/badge/Python-182/2081-1abc9c.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-183/2081-1f425f.svg)
4+
![problems-solved-java](https://img.shields.io/badge/Java-183/2081-1abc9c.svg)
5+
![problems-solved-python](https://img.shields.io/badge/Python-183/2081-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77
[![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming)
88

@@ -206,7 +206,7 @@
206206
| 744 | [Find Smallest Letter Greater Than Target](https://leetcode.com/problems/find-smallest-letter-greater-than-target) | [![Java](assets/java.png)](src/FindSmallestLetterGreaterThanTarget.java) [![Python](assets/python.png)](python/find_smallest_letter_greater_than.py) |
207207
| 746 | [Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs) | [![Java](assets/java.png)](src/MinCostClimbingStairs.java) [![Python](assets/python.png)](python/min_cost_climbing_stairs.py) |
208208
| 747 | [Largest Number at least twize of Others](https://leetcode.com/problems/largest-number-at-least-twice-of-others) | [![Java](assets/java.png)](src/LargestNumberAtLeastTwiceOfOthers.java) [![Python](assets/python.png)](python/largest_number_at_least_twice_of_others.py) |
209-
| 748 | [Shortest Completing Word](https://leetcode.com/problems/shortest-completing-word) | |
209+
| 748 | [Shortest Completing Word](https://leetcode.com/problems/shortest-completing-word) | [![Java](assets/java.png)](src/ShortestCompletingWord.java) [![Python](assets/python.png)](python/shortest_completing_word.py) |
210210
| 758 | [Bold Words in String](https://leetcode.com/problems/bold-words-in-string) | |
211211
| 760 | [Find Anagram Mappings](https://leetcode.com/problems/find-anagram-mappings) | |
212212
| 762 | [Prime Number of Set Bits in Primary Representation](https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation) | |

python/shortest_completing_word.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def __init__(self):
6+
self.primes = [
7+
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
8+
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101
9+
]
10+
11+
def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str:
12+
licenseHash = self.getHash(licensePlate)
13+
resultIndex, shortestLen = -1, float('inf')
14+
for index, word in enumerate(words):
15+
if len(word) < shortestLen and self.coversAllChars(word, licenseHash):
16+
resultIndex = index
17+
shortestLen = len(word)
18+
return words[resultIndex]
19+
20+
def getHash(self, string: str) -> int:
21+
result = 1
22+
for char in string.lower():
23+
if char.isalpha():
24+
result *= self.primes[ord(char) - ord('a')]
25+
return result
26+
27+
def coversAllChars(self, word: str, hash: int) -> bool:
28+
result = 1
29+
for character in word:
30+
result = (result * self.primes[ord(character) - ord('a')]) % hash
31+
return result == 0

src/ShortestCompletingWord.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
public class ShortestCompletingWord {
2+
private final int[] primes = {
3+
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
4+
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101
5+
};
6+
7+
public String shortestCompletingWord(String licensePlate, String[] words) {
8+
long licenseHash = getHash(licensePlate);
9+
int shortestCompletingWordLen = Integer.MAX_VALUE;
10+
int shortestWordIndex = -1;
11+
for (int index = 0 ; index < words.length ; index++) {
12+
if (words[index].length() < shortestCompletingWordLen && containsAllChars(words[index], licenseHash)) {
13+
shortestCompletingWordLen = words[index].length();
14+
shortestWordIndex = index;
15+
}
16+
}
17+
return words[shortestWordIndex];
18+
}
19+
20+
private long getHash(String string) {
21+
long result = 1;
22+
for (char character : string.toLowerCase().toCharArray()) {
23+
if (Character.isAlphabetic(character)) {
24+
result *= primes[character - 'a'];
25+
}
26+
}
27+
return result;
28+
}
29+
30+
private boolean containsAllChars(String word, long hash) {
31+
long result = 1;
32+
for (char character : word.toCharArray()){
33+
result = (result * primes[character - 'a']) % hash;
34+
}
35+
return result == 0;
36+
}
37+
}

0 commit comments

Comments
 (0)