본문 바로가기

python-algorithm1422

leetcode 290. Word Pattern 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class Solution: def wordPattern(self, pattern: str, s: str) -> bool: s=list(s.split()) checkList=[] checkPattern=[] ans='' ansPattern='' for i in s: if i in checkList : pass else: checkList.append(i) ans += chr(checkList.index(i) + 97) for i in pattern: if i in checkPattern : pass else: checkPattern.append(i) ansPattern += chr(checkPattern.in.. 2022. 3. 17.
leetcode 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence 1 2 3 4 5 6 7 8 9 class Solution: def isPrefixOfWord(self, sentence: str, searchWord: str) -> int: sentence=list(sentence.split()) searchWordLength=len(searchWord) index=-1 for i in sentence: if len(i)>=len(searchWord): if i[:searchWordLength]==searchWord : index=sentence.index(i)+1;break return (index) Colored by Color Scripter cs 2022. 3. 17.
leetcode 2185. Counting Words With a Given Prefix 1 2 3 4 5 6 7 class Solution: def prefixCount(self, words: List[str], pref: str) -> int: prefLength=len(pref) cnt=0 for i in words: if i[:prefLength]==pref : cnt+=1 return cnt Colored by Color Scripter cs 2022. 3. 17.
leetcode 557. Reverse Words in a String III 1 2 3 4 5 6 7 class Solution: def reverseWords(self, s: str) -> str: s=list(s.split()) ans='' for i in s: ans+=i[::-1]+' ' return ans[:-1] cs 2022. 3. 17.
leetcode 2000. Reverse Prefix of Word 1 2 3 4 5 6 7 8 9 class Solution: def reversePrefix(self, word: str, ch: str) -> str: try: that=word.index(ch) word=word[:that+1][::-1]+word[word.index(ch)+1:] #return newWord except : #return word pass return word Colored by Color Scripter cs 2022. 3. 16.
leetcode 1704. Determine if String Halves Are Alike 1 2 3 4 5 6 7 8 9 10 11 12 13 class Solution: def halvesAreAlike(self, s: str) -> bool: vowel=['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] half1=s[:len(s)//2] half2=s[len(s)//2:] half1Cnt=0 for i in half1: if i in vowel: half1Cnt+=1 half2Cnt=0 for i in half2: if i in vowel: half2Cnt+=1 if half1Cnt==half2Cnt : return True else: return False Colored by Color Scripter cs 2022. 3. 16.
leetcode 1464. Maximum Product of Two Elements in an Array 1 2 3 4 5 6 7 class Solution: def maxProduct(self, nums: List[int]) -> int: first=max(nums) nums.pop(nums.index(first)) second=max(nums) ans=(first-1)*(second-1) return ans cs 2022. 3. 16.
leetcode 125. Valid Palindrome 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 def isPalindromic(s): ans=False convertedS='' for i in s: i=i.lower() #i=i.replace(" ",'') if i.isalnum(): convertedS+=i sLength=len(convertedS) if sLength%2==0: if convertedS[:sLength//2] ==convertedS[sLength//2:][::-1] :ans=True else: if convertedS[:sLength//2] ==convertedS[sLength//2+1:][::-1] :ans=True return ans class Solution: def isPalindrome(self, s.. 2022. 3. 15.
leetcode 2108. Find First Palindromic String in the Array 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 def isPalindromic(s): ans='' sLength=len(s) if sLength%2==0: if s[:sLength//2] ==s[sLength//2:][::-1] :ans=s else: if s[:sLength//2] ==s[sLength//2+1:][::-1] :ans=s return ans class Solution: def firstPalindrome(self, words: List[str]) -> str: ans='' for i in words: if isPalindromic(i)!='' : ans=isPalindromic(i) break return ans Colored by Color.. 2022. 3. 15.
leetcode 2176. Count Equal and Divisible Pairs in an Array 1 2 3 4 5 6 7 class Solution: def countPairs(self, nums: List[int], k: int) -> int: ans=0 for i in range(len(nums)): for j in range(i+1,len(nums)): if nums[i]==nums[j] and i*j%k==0 :ans+=1 return ans Colored by Color Scripter cs 2022. 3. 15.
leetcode 1844. Replace All Digits with Characters 1 2 3 4 5 6 7 class Solution: def replaceDigits(self, s: str) -> str: ans='' for i in range(len(s)): if s[i].isalpha(): ans+=s[i] if s[i].isdigit(): ans+=chr(ord(s[i-1])+int(s[i])) return ans cs 2022. 3. 15.
leetcode 2089. Find Target Indices After Sorting Array 1 2 3 4 5 6 7 8 class Solution: def targetIndices(self, nums: List[int], target: int) -> List[int]: ans=[] nums.sort() for i in range(len(nums)): if target==nums[i]: ans.append(i) #if target 2022. 3. 15.