This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |