Skip to content

Instantly share code, notes, and snippets.

View pratik7368patil's full-sized avatar

Pratik Patil pratik7368patil

  • Maharashtra, India
View GitHub Profile
@pratik7368patil
pratik7368patil / all_permutations.py
Created May 30, 2020 11:03
Find all permutations
# 1. Method using Backtracking
def all_permutations(arr, l):
r = len(string) - 1 # right pointer
if l == r:
print("".join(arr))
for i in range(l,r+1):
@pratik7368patil
pratik7368patil / count_v_c.py
Created May 30, 2020 10:11
Count Vowel and Consonents
# here is the solution for given problem
def count(string):
vowel = 0 # vowel counter
cons = 0 # consonants counter
temp = string.lower() # convert string to lower
v = ["a","e","i","o","u"] # defining vowels
for i in temp:
if i in v: # check for vowel if found increment vowel counter
vowel += 1
@pratik7368patil
pratik7368patil / count_given_char.py
Created May 29, 2020 17:44
Count given Character in String
# this problem can be solve using lot's of methods
# 1. Native Method
# By visiting each character in string
def count_char(string, char):
count = 0 # declearing veriable to count char
for i in string:
if i == char:
@pratik7368patil
pratik7368patil / check_palindrome.py
Last active May 29, 2020 17:20
Check Palindrome String
# if you know what is palindrome means and how to reverse a string
# then you know answer
# 1. Method using string slicing
def check_palindrome(string):
# you can not compair case sensitive strings
string = string.lower()
if string == string[::-1]:
return True
else:
@pratik7368patil
pratik7368patil / check_anagram.py
Created May 29, 2020 16:26
Check if two strings are Anagram
# this problem can be solve using Hashmap with O(N) time
def check_anagram(s1, s2):
if len(s1) != len(s2): # check for base case
return False
counter = [0]*26 # each character of string is between a-z
n = ord("a") # integer value of "a" that is 97
for i in range(len(s1)):
counter[ord(s1[i]) - n] += 1 # update counter at each character
counter[ord(s2[i]) - n] -= 1 # decrease counter at each character
@pratik7368patil
pratik7368patil / duplicate_in_string.py
Created May 29, 2020 15:47
Find Duplicate from String
# This problem can be solve with O(N) time
# but it require some extra space
# to make it more simple we can use collection module
# you can also manually add records in Hashmap or Dictionary if you don't want to use module
from collections import Counter
def find_duplicate(s):
d = Counter(s) # count each character ----O(N)
# it's very easy to remove white spaces from strings
# it can be done using many methods but here we will disscuss some efficient one
# 1. Using replace()
s = " h a c k you r co de " # this function doesn't modify original string
new_s = s.replace(" ","") # here new_s holds new string with no blank spaces at all.
print(new_s)
# the output will be "hackyourcode"
# 2. Using join() and split()
@pratik7368patil
pratik7368patil / find_peak.py
Created May 28, 2020 17:57
Finding the Peak Element from an Array
# we easily solve this using binary search algorithm with O(logN) time.
def find_peak(arr):
l,r = 0,len(arr) - 1
while l < r:
m = (l+r)//2 # median of an array
if arr[m] < arr[m+1]: # looking for the peak element
l = m + 1
else:
r = m
@pratik7368patil
pratik7368patil / search_r_s.py
Last active May 28, 2020 17:40
Search in Rotated Sorted Array
# searching for element in rotated sorted array
# this can be solve using binary search algorithm which takes O(logn) time to perform
# idea is if arr[mid] < arr[-1] then right side is sorted else left side is sorted
def search(arr, target):
start, end = 0, len(arr) - 1
while start <= end:
mid = (start + end) // 2
if arr[mid] == target:
@pratik7368patil
pratik7368patil / max_min.py
Created May 28, 2020 17:03
Find Maximum and Minimum Number from Unsorted Array
# our task is to find minimum and maximum number from array in one just one go
# the complexity of this program is O(n)
def min_max(arr):
maximum = arr[0] # assign random value from array as max
minimum = arr[0] # assign random value from array as min
for num in arr:
if num > maximum:
maximum = num