Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Leetcode

Uploaded by

Ashie Aishu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Leetcode

Uploaded by

Ashie Aishu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#Reverse string

Write a function that reverses a string. The input string is given as an array of
characters s.
You must do this by modifying the input array in-place with O(1) extra memory.

Example 1:
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Example 2:
Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
Constraints:
1 <= s.length <= 105

s[i] is a printable ascii character.

code:
class Solution:

def reverseString(self, s: List[str]) -> None:


left, right = 0, len(s) - 1
while left < right:
s[left], s[right] = s[right], s[left]
left += 1
right -= 1

#append characters to string to make a subsequence


You are given two strings s and t consisting of only lowercase English letters.
Return the minimum number of characters that need to be appended to the end
of s so that t becomes a subsequence of s.
A subsequence is a string that can be derived from another string by deleting
some or no characters without changing the order of the remaining characters.
Example 1:
Input: s = "coaching", t = "coding"
Output: 4
Explanation: Append the characters "ding" to the end of s so that s =
"coachingding".
Now, t is a subsequence of s ("coachingding").
It can be shown that appending any 3 characters to the end of s will never make
t a subsequence.
Example 2:
Input: s = "abcde", t = "a"
Output: 0
Explanation: t is already a subsequence of s ("abcde").
Example 3:
Input: s = "z", t = "abcde"
Output: 5
Explanation: Append the characters "abcde" to the end of s so that s = "zabcde".
Now, t is a subsequence of s ("zabcde").
It can be shown that appending any 4 characters to the end of s will never make
t a subsequence.
Constraints:
1 <= s.length, t.length <= 105
s and t consist only of lowercase English letters.
Code:

class Solution:
def appendCharacters(self, s: str, t: str) -> int:
i, j = 0, 0
while i < len(s) and j < len(t):
if s[i] == t[j]:
j += 1
i += 1
return len(t) – j

#Longest palindrome

Given a string s which consists of lowercase or uppercase letters, return the


length of the longest
palindrome
that can be built with those letters.
Letters are case sensitive, for example, "Aa" is not considered a palindrome.
Example 1:
Input: s = "abccccdd"
Output: 7
Explanation: One longest palindrome that can be built is "dccaccd", whose length
is 7.
Example 2:
Input: s = "a"
Output: 1
Explanation: The longest palindrome that can be built is "a", whose length is 1.
Constraints:
1 <= s.length <= 2000
s consists of lowercase and/or uppercase English letters only.
Code:

class Solution:
def longestPalindrome(self, s: str) -> int:
char_count = {}
for char in s:
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1

length = 0
odd_count = 0
for count in char_count.values():
if count % 2 == 0:
length += count
else:
length += count - 1
odd_count += 1

if odd_count > 0:
length += 1

return length

Given a string array words, return an array of all characters that show up in all
strings within the words (including duplicates). You may return the answer in any
order.

Example 1:
Input: words = ["bella","label","roller"]
Output: ["e","l","l"]
Example 2:
Input: words = ["cool","lock","cook"]
Output: ["c","o"]
Constraints:
1 <= words.length <= 100
1 <= words[i].length <= 100
words[i] consists of lowercase English letters.
Code:

class Solution:
def commonChars(self, words: List[str]) -> List[str]:
if not words:
return []

common_chars = set(words[0])
for word in words[1:]:
common_chars &= set(word)

result = []
for char in common_chars:
count = min(word.count(char) for word in words)
result.extend([char] * count)

return result

You might also like