This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
class Solution: | |
def maxProduct(self, words: List[str]) -> int: | |
hashmap = defaultdict(int) | |
bit_number = lambda ch : ord(ch) - ord('a') | |
for word in words: | |
bitmask = 0 | |
for ch in word: | |
# add bit number bit_number in bitmask |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int bitNumber(char ch){ | |
return (int)ch - (int)'a'; | |
} | |
public int maxProduct(String[] words) { | |
Map<Integer, Integer> hashmap = new HashMap(); | |
int bitmask = 0, bitNum = 0; | |
for (String word : words) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
func maxProduct(_ words: [String]) -> Int { | |
var maxLength = 0 | |
var bitMap = [Int:Int]() | |
for word in words{ | |
let bitMask = getBitMask(word) | |
// Multiple words can have same bitmasks (a , aaaaaa). | |
// We need to keep the max length only | |
if let currentLength = bitMap[bitMask] { | |
bitMap[bitMask] = max(currentLength, word.count) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
func minPartitions(_ n: String) -> Int { | |
var maxCharacter = 0 | |
for c in n { | |
maxCharacter = max(maxCharacter,Int(String(c),radix:10)!) | |
} | |
return maxCharacter | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def minPartitions(self, n: str) -> int: | |
return int(max(n)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int minPartitions(String n) { | |
int maximum = 0; | |
for (char c : n.toCharArray()) { | |
maximum = Math.max(maximum, c - '0'); | |
} | |
return maximum; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
func evalRPN(_ tokens: [String]) -> Int { | |
var stack = [Int]() | |
var operators = "+-/*" | |
for token in tokens { | |
if operators.contains(token) { | |
var result = 0 | |
let second = stack.popLast()! | |
let first = stack.popLast()! | |
switch token { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def evalRPN(self, tokens): | |
stack = [] | |
for token in tokens: | |
if token not in "+-/*": | |
stack.append(int(token)) | |
continue | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
private static final Map<String, BiFunction<Integer, Integer, Integer>> OPERATIONS = new HashMap<>(); | |
// Ensure this only gets done once for ALL test cases. | |
static { | |
OPERATIONS.put("+", (a, b) -> a + b); | |
OPERATIONS.put("-", (a, b) -> a - b); | |
OPERATIONS.put("*", (a, b) -> a * b); | |
OPERATIONS.put("/", (a, b) -> a / b); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
func toLowerCase(_ s: String) -> String { | |
var result = "" | |
for c in s { | |
var letter = c | |
if c >= "A", c <= "Z" { | |
letter = toLower(c) | |
} | |
result += "\(letter)" | |
} |