Skip to content

Difficulty - Easy

Just a moment...

Solution in Python
from typing import List


class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        lengths = [len(s) for s in strs]
        if 0 in lengths or not lengths:
            return ""
        else:
            result = ""
            index = 0

            while index < min(lengths):
                character = set([s[index] for s in strs])
                if len(character) > 1:
                    break
                else:
                    result += character.pop()
                    index += 1

            return result

Just a moment...

Solution in Python
class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x < 0:
            return False

        a = 1
        while x // a:
            a *= 10
        a //= 10

        while x:
            r = x % 10
            l = x // a
            if l != r:
                return False
            x = (x - l * a) // 10
            a = a // 100

        return True

Just a moment...

Solution in Python
class Solution:
    def romanToInt(self, s: str) -> int:
        roman = {"M": 1000, "D": 500, "C": 100, "L": 50, "X": 10, "V": 5, "I": 1}
        size = len(s)
        prev = None
        ans = 0
        s = s[::-1]
        for i in range(size):
            if prev and roman[s[i]] < prev:
                ans -= 2 * roman[s[i]]
            ans += roman[s[i]]
            prev = roman[s[i]]
        return ans

Just a moment...

Solution in Python
from typing import List


class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        t = {}
        l = len(nums)

        for i in range(l):
            if nums[i] not in t:
                t[nums[i]] = [i]
            else:
                t[nums[i]].append(i)

        for i in range(l):
            lookup = target - nums[i]
            if lookup != nums[i]:
                if lookup in t:
                    return (i, t[lookup][0])
            else:
                if len(t[lookup]) > 1:
                    return (t[lookup][0], t[lookup][1])

Just a moment...

Solution in Python
class Solution:
    def isValid(self, s: str) -> bool:
        stack = []
        for char in s:
            if char in ["(", "[", "{"]:
                stack.append(char)
            elif char in [")", "]", "}"]:
                if len(stack) == 0:
                    return False
                last = stack.pop()
                if char == ")" and last != "(":
                    return False
                elif char == "]" and last != "[":
                    return False
                elif char == "}" and last != "{":
                    return False

        return len(stack) == 0