0% found this document useful (0 votes)
2 views3 pages

Anagram, Palindrome, and Integer Reverse Functions

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Anagram, Palindrome, and Integer Reverse Functions

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

[Link] two strings s and t, return true if t is an anagram of s, and false otherwise.

Example 1:

Input: s = "anagram", t = "nagaram"

Output: true

Example 2:

Input: s = "rat", t = "car"

Output: false

Code:

def isAnagram(self, s, t):

x=sorted(list(s))

y=sorted(list(t))

if x==y:

return True

return False

2. A phrase is a palindrome if, after converting all uppercase letters into lowercase letters
and removing all non-alphanumeric characters, it reads the same forward and backward.
Alphanumeric characters include letters and numbers.

Given a string s, return true if it is a palindrome, or false otherwise.

Example 1:

Input: s = "A man, a plan, a canal: Panama"

Output: true

Explanation: "amanaplanacanalpanama" is a palindrome.

Example 2:

Input: s = "race a car"

Output: false

Explanation: "raceacar" is not a palindrome.


Code:

def isPalindrome(self, s):

cleaned = ''

for char in s:

if [Link]():

cleaned += [Link]()

return cleaned == cleaned[::-1]

Reversing of an integer:
code:

def reverse(self, x):

reverse = 0

negative = False

if x < 0:

negative = True

x = -x

while x > 0:

last_digit = x % 10

reverse = (reverse * 10) + last_digit

x = x // 10

if negative:

reverse = -reverse
if reverse < -2*31 or reverse > (2*31 - 1):

return 0

return reverse

4. if not matrix or not matrix[0]:

return False

rows = len(matrix)

cols = len(matrix[0])

for i in range(rows):

for j in range(cols):

if matrix[i][j] == target:

return True

return False

You might also like