The Programming Project: LeetCode
Showing posts with label LeetCode. Show all posts
Showing posts with label LeetCode. Show all posts

Wednesday, January 25, 2023

Leet Code Rotate Image

Leet Code Rotate Image 

You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).


Example 1:

Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]

Example 2:

Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]

 

Constraints:

  • n == matrix.length == matrix[i].length
  • 1 <= n <= 20
  • -1000 <= matrix[i][j] <= 1000





class Solution:
    def rotate(self, matrix):
        order = len(matrix[0])
        newmatrix = []
        for i in range(order):
            newmatrix.append([])
            for j in range(order):
                newmatrix[i].append(0)
        newColoumn = []
        for i in range(order):
            newColoumn.append(order-1-i)
        for i in range(order):
            for j in range(order):
                newmatrix[j][newColoumn[i]] = matrix[i][j]
        for i in range(order):
            for j in range(order):
                matrix[i][j] = int(newmatrix[i][j])
        print(matrix)
obj = Solution()
#change the matrix here
matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
obj.rotate(matrix)

Leet Code Longest Substring Without Repeating Characters

Given a string s, find the length of the longest 

 without repeating characters.

 

Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.

 

Constraints:

  • 0 <= s.length <= 5 * 104
  • s consists of English letters, digits, symbols and spaces.

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        maxLength = 0
        for i in range(len(s)):
            substring = []
            for j in range(i,len(s)):
                if s[j] in substring:
                    break
                else:
                    substring.append(s[j])
            if len(substring) > maxLength:
                maxLength = len(substring)
        return maxLength
obj = Solution()
s = str(input("Enter a string:"))
print(obj.lengthOfLongestSubstring(s))

Monday, January 23, 2023

Leet Code ZigZag Conversion

 





The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P   A   H   N
A P L S I I G
Y   I   R

And then read line by line: "PAHNAPLSIIGYIR"

Write the code that will take a string and make this conversion given a number of rows:

string convert(string s, int numRows);

 

Example 1:

Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"

Example 2:

Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P     I    N
A   L S  I G
Y A   H R
P     I

Example 3:

Input: s = "A", numRows = 1
Output: "A"

 

Constraints:

  • 1 <= s.length <= 1000
  • s consists of English letters (lower-case and upper-case), ',' and '.'.
  • 1 <= numRows <= 1000




import numpy as np


class Solution(object):

    def convert(self, s, numRows):
        numCols = len(s)
        arr_characters = np.zeros(numRows * numCols).reshape(numRows, numCols)
        #print(arr_characters)
        if numRows == 1:
            return s
        else:

            counter = 0
            pos = 0
            vertical_shift = 0
            for i in range(len(s)):
                if counter <= numRows:
                    _zero = 0
                    for j in range(numRows):
                        if pos >= len(s):
                            break
                        else:
                            #print(s[pos],end=' ')
                            #print("(",_zero,vertical_shift,")",end= ' ') # = ord(s[pos])
                            arr_characters[_zero][vertical_shift] = ord(s[pos])
                            pos += 1
                            _zero += 1
                    counter = numRows + 1
                    vertical_shift += (numRows - 1)
                    #print(vertical_shift,"***************")

                else:
                    diagonal_shift_col = vertical_shift - (numRows - 2)
                    diagonal_shift_row = numRows - 2
                    for j in range(numRows - 2):
                        if pos >= len(s):
                            break
                        else:
                            #print(s[pos],end = ' ')
                            #print("(",diagonal_shift_row,diagonal_shift_col,")",end= ' ') # = ord(s[pos])
                            arr_characters[diagonal_shift_row][diagonal_shift_col] = ord(s[pos])
                            diagonal_shift_col += 1
                            diagonal_shift_row -= 1
                            pos += 1
                    #print("**************")
                    counter = 0
            #print(arr_characters)
            output = ""
            for i in range(numRows):
                for j in range(numCols):
                    if arr_characters[i][j] != 0:
                        output += chr(int(arr_characters[i][j]))
                    else:
                        output += ""
            #output.encode('utf-8')
            return output


obj = Solution()
s = ""
numRows = int(input("Enter the number of rows:"))
s = str(input("Enter the string:"))
print(obj.convert(s, numRows))

Leet Code Two Sum


Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

 

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]

Example 3:

Input: nums = [3,3], target = 6
Output: [0,1]

 

Constraints:

  • 2 <= nums.length <= 104
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
  • Only one valid answer exists.


class Solution(object):
    def twoSum(self, nums, target):
        output = []
        flag = False
        for i in range(len(nums)):
            for j in range (i+1,len(nums)):
                if nums[i]+nums[j] == target:
                    output.append(i)
                    output.append(j)
                    flag = True
                    break
            if flag == True:
                break
        print(output)
        #return(output)
obj = Solution()
nums = []
target = int(input("Enter the target element:"))
n = int(input("Enter the number of elements in the array:"))
for i in range(n):
    nums.append(int(input("Enter an element:")))
print(nums)
obj.twoSum(nums,target)