Monday, November 6, 2017

242. Valid Anagram

class Solution:
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        def construct_dict(a_string):
            a_dict={}
            for i in range(len(a_string)):
                if a_string[i] in a_dict:
                    a_dict[a_string[i]] += 1
                else:
                    a_dict[a_string[i]] = 1
            return a_dict
        dict_1 = construct_dict(s)
        dict_2 = construct_dict(t)
        if dict_1 == dict_2:
            return True
        else:
            return False

58. Length of Last Word

class Solution:
    def lengthOfLastWord(self, s):
        """
        :type s: str
        :rtype: int
        """
        new_string = s
        new_string = new_string.lower()
        new_list = new_string.split()
        try:
            return len(new_list[-1])
        except:
            return 0

Sunday, November 5, 2017

7. Reverse Integer

class Solution:
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        value = 0
        if x >= 2147483647 or x <= -2147483647:
            return 0
        try:
            if x > 0:
                value = int(str(x)[::-1])
            elif x < 0:
                value = -int(str(-x)[::-1])
            if value >= 2147483647 or value <= -2147483647:
                return 0
            return value
        except OverflowError:
            return 0

125. Valid Palindrome

class Solution:
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        if s == "":
            return True
        else:
            new_string = s
            new_string = new_string.lower()
            new_list = []
            for i in new_string:
                if (ord(i) >= 97 and ord(i) <= 122) or (ord(i) >= 48 and ord(i) <= 57):
                    new_list.append(i)
            new_string = ("").join(new_list)
            print (new_string)
            if new_string == new_string[::-1]:
                return True
            else:
                return False

290. Word Pattern

class Solution:
    def wordPattern(self, pattern, str):
        """
        :type pattern: str
        :type str: str
        :rtype: bool
        """
        a_dict = {}
        new_list = str.split()
        if len(pattern) != len(new_list):
            return False
        else:
            for i in range(len(pattern)):
                if pattern[i] in a_dict:
                    if a_dict[pattern[i]] != new_list[i]:
                        return False
                else:
                    if new_list[i] in a_dict.values():
                        return False
                    else:
                        a_dict[pattern[i]] = new_list[i]
        return True  

205. Isomorphic Strings

class Solution:
    def isIsomorphic(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        a_dict = {}
        if len(s) != len(t):
            return False
        else:
            for i in range(len(s)):
                if s[i] in a_dict:
                    if a_dict[s[i]] != t[i]:
                        return False
                else:
                    if t[i] in a_dict.values():
                        return False
                    else:
                        a_dict[s[i]] = t[i]                   
        return True     

Saturday, November 4, 2017

434. Number of Segments in a String

class Solution:
    def countSegments(self, s):
        """
        :type s: str
        :rtype: int
        """
        new_list = []
        new_list = s.split()
        return len(new_list)