Found 33676 Articles for Programming

Program to equal two strings of same length by swapping characters in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:25:37

609 Views

Suppose we have two strings s and t of length n. We can take one character from s and another from t and swap them. We can make unlimited number of swaps; we have to check whether it's possible to make the two strings equal or not.So, if the input is like s = "xy", t = "yx", then the output will be TrueTo solve this, we will follow these steps −st:= sort the string after concatenating s and tfor i in range 0 to size of st - 1, increase by 2, doif st[i] is not same as st[i+1], ... Read More

Program to find length of substring with consecutive common characters in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:23:26

542 Views

Suppose we have a string s, we have to find the length of the longest substring with same characters.So, if the input is like "abbbaccabbbba", then the output will be 4, as there are four consecutive b's.To solve this, we will follow these steps −if size of s is 0, thenreturn 0s := s concatenate blank spacect:= 1, tem:= 1for i in range 0 to size of s -2, doif s[i] is same as s[i+1], thentem := tem + 1otherwise, ct:= maximum of tem and cttem:= 1return ctLet us see the following implementation to get better understanding −Example Live Democlass Solution: ... Read More

Program to find longest common prefix from list of strings in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:21:16

2K+ Views

Suppose we have a list of lowercase strings, we have to find the longest common prefix.So, if the input is like ["antivirus", "anticlockwise", "antigravity"], then the output will be "anti"To solve this, we will follow these steps −sort the list words alphabeticallyprefix := a new listflag := 0for i in range 0 to size of words[0], dofor each j in words, doif j[i] is not same as last element of prefix, thendelete last element from prefixflag := 1come out from the loopif flag is same as 1, thencome out from the loopreturn string after concatenating all elements present in prefix ... Read More

Program to replace each element by smallest term at left side in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:19:25

249 Views

Suppose we have a list of numbers called nums, we have to replace every nums[i] with the smallest element left of i. We have to replace nums[0] with 0.So, if the input is like [15, 7, 9, 16, 12, 25], then the output will be [0, 15, 7, 7, 7, 7]To solve this, we will follow these steps −if nums is empty, thenreturn a new listj:= nums[0]nums[0]:= 0for i in range 1 to size of nums - 1, dok:= nums[i]nums[i]:= jj:= minimum of j, kreturn numsLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def ... Read More

Program to make all elements equal by performing given operation in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:17:37

526 Views

Suppose we have given a list of numbers called nums, we want to make the values equal. Now let an operation where we pick one element from the list and increment every other value. We have to find the minimum number of operations required to make element values equal.So, if the input is like [2, 4, 5], then the output will be 5.To solve this, we will follow these steps −min_val := minimum of numss := 0for each num in nums, dos := s + (num - min_val)return sLet us see the following implementation to get better understanding −Example Live Democlass ... Read More

Program to perform given operation with each element of a list and given value in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:16:14

361 Views

Suppose we have a list of numbers called nums, we also have another string op representing operator like "+", "-", "/", or "*", and another value val is also given, we have to perform the operation on every number in nums with val and return the result.So, if the input is like [5, 3, 8], then the output will be [15, 9, 24]To solve this, we will follow these steps −res:= a new listfor each i in nums, doif op is same as '+', theninsert i+val at the end of resotherwise when op is same as '-', theninsert i-val at ... Read More

Program to convert Linked list representing binary number to decimal integer in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:14:23

2K+ Views

Suppose we have a singly linked list. the linked list is representing a binary number with most significant digits first, we have to return it as decimal number.So, if the input is like [1, 0, 1, 1, 0], then the output will be 22To solve this, we will follow these steps −l := a new listwhile node is not null, doinsert value of node at the end of lnode:= next of nodek := 0, v:= 0for i in range size of l - 1 to 0, decrease by 1, doif l[i] is same as 1, thenv := v + 2^kk ... Read More

Program to remove all nodes of a linked list whose value is same as in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:08:18

415 Views

Suppose we have a singly linked list, and one target, we have to return the same linked after deleting all nodes whose value is same as target.So, if the input is like [5, 8, 2, 6, 5, 2, 9, 6, 2, 4], then the output will be [5, 8, 6, 5, 9, 6, 4, ]To solve this, we will follow these steps −head := nodewhile node and node.next are not null, dowhile value of next of node is same as target, donext of node := next of next of nodenode := next of nodeif value of head is same as ... Read More

Program to find Lexicographically Smallest String With One Swap in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:06:36

3K+ Views

Suppose we have a string s, we have to find the lexicographically smallest string that can be made if we can make at most one swap between two characters in the given string s.So, if the input is like "zyzx", then the output will be "xyzz"To solve this, we will follow these steps −temp := an array of size s and fill with 0m:= size of s - 1for i in range size of s -1 to -1, decrease by 1, doif s[i] < s[m], thenm := itemp[i] := mfor i in range 0 to size of s, doa := ... Read More

Program to check whether a binary tree is BST or not in Python

Arnab Chakraborty
Updated on 06-Oct-2020 05:59:22

665 Views

Suppose we have binary tree; we have to check whether it is a binary search tree or not. As we know a BST has following properties −all nodes on its left subtree is smaller than current node valueall nodes on its right subtree is larger than current node valuethese properties hold recursively for all nodesSo, if the input is likethen the output will be TrueTo solve this, we will follow these steps −x := a list of inorder traversal sequence of tree elementsif x is sorted, thenreturn truereturn falseLet us see the following implementation to get better understanding −Example Live Democlass ... Read More

Advertisements