Posts

Leet code : Find the index of First Occurence in String

Question:  https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/description/ Solution: class Solution: def strStr(self, haystack: str, needle: str) -> int: if needle in haystack: return haystack.index(needle) else: return -1

Leetcode : 3Sum

 Questino:- https://leetcode.com/problems/3sum/ Solution :  import numpy as np class Solution :     def threeSum ( self , nums : List[ int ]) -> List[List[ int ]]:         list_length = len (nums)         final_list = []         final_list_str = []         if list_length< 3 :             return []         elif list_length == 3 :             if sum (nums)== 0 :                 return [nums]             else :                 return []         else :             for len_nest1 in range ( 0 ,list_length- 2 ):                 for len_nest2 in range (len_nest1+ 1 ,list_length- 1 ):    ...

LeetCode: Container with Most Water

Question :- https://leetcode.com/problems/container-with-most-water/description/ Solution:- class Solution :     def maxArea ( self , height ) -> int :         len_height = len (height)         max_area = 0         for hgt in range ( 0 ,len_height):             for sub_hgt in range ( 0 ,len_height):                 if sub_hgt ==hgt:                     continue                 else :                     if height[hgt] <= height[sub_hgt]:                         area = height[hgt]* abs (hgt - sub_hgt)                         if max_area<area:           ...

LeetCode: Reverse Integer

Question :  Given a signed 32-bit integer   x , return   x  with its digits reversed . If reversing   x   causes the value to go outside the signed 32-bit integer range   [-2 31 , 2 31 - 1] , then return   0 . Solution : class Solution: def reverse(self, x: int) -> int: convert_str = str(x) if "-" in convert_str: convert_str = convert_str[1:] converted_int = "-"+convert_str[::-1] else: converted_int = convert_str[::-1] converted_int = int(converted_int) if converted_int >= (2 ** 31 -1) or converted_int <= (-2 ** 31 -1): return 0 else: return converted_int

LeetCode : ZigZag Conversion

 Question :- https://leetcode.com/problems/zigzag-conversion/ Solution :-  class Solution :     def convert (self, s: str , numRows: int ) -> str :         converted_str = ""         start_index = 0         index_num = numRows + numRows - 1 - 1         for i in range ( 0 ,numRows):             while (start_index < len (s)):                 converted_str += s[start_index]                 start_index = start_index + index_num             start_index = i             index_num = index_num - 2             print (converted_str)         return converted_str s = "PAYPALISHIRING" number = 3 ss = Solution() print (ss.convert(s,number))

LeetCode: Longest Substring Without Repeating Characters

Question: https://leetcode.com/problems/longest-substring-without-repeating-characters/ Solution: 1 Test Case Failed Probable Solution, Need to remove the For loop class Solution :     def lengthOfLongestSubstring ( self , s : str ) -> int :         len_s = len (s)         max_sub = 0         for character in range ( 0 ,len_s):             for sub_character in range (character,len_s+ 1 ):                 if len ( set (s[character:sub_character])) == len (s[character:sub_character]) and len (s[character:sub_character]) > max_sub:                     max_sub = len (s[character:sub_character])         return max_sub

Codechef: Find the Maximum Value

Question: The Chef had a box with  N  numbers arranged inside it:  A 1 ,  A 2 , ...,  A N . He also had the number  N  at the front, so that he knows how many numbers are in it. That is, the box actually contains  N +1 numbers. But in his excitement due the ongoing  IOI , he started dancing with the box in his pocket, and the  N +1 numbers got jumbled up. So now, he no longer knows which of the  N +1 numbers is  N , and which the actual numbers are. He wants to find the largest of the  N  numbers. Help him find this. Input The first line of the input contains an integer  T , denoting the number of test cases. The description of each testcase follows. Each of the next  T  lines will contain  N  and  N  numbers, but it is not guaranteed that  N  is the first number. Output For each test case, output a single line containing the maximum value of the  N  numbers in th...