Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 531 of 2547
Program to check whether one string can be 1-to-1 mapped into another string in Python
Sometimes we need to check whether one string can be mapped to another string with a 1-to-1 character mapping. This means each character in the first string maps to exactly one character in the second string, and vice versa. For example, if we have strings "papa" and "lili", we can create a valid mapping: "p" → "l" and "a" → "i". The character order remains unchanged. Algorithm To solve this problem, we use two dictionaries to track mappings in both directions ? Create two dictionaries: one for mapping characters from string s to t, another ...
Read MoreProgram to check whether one value is present in BST or not in Python
A Binary Search Tree (BST) is a tree data structure where each node has at most two children, and for each node, all values in the left subtree are smaller and all values in the right subtree are larger. We can efficiently search for a value by comparing it with the current node and moving left or right accordingly. .node { fill: #e8f4fd; stroke: #2196f3; stroke-width: 2; } .text { font-family: Arial; font-size: 14px; text-anchor: middle; fill: #333; ...
Read MoreProgram to check one string can be converted to other by shifting characters clockwise in Python
When working with strings, we sometimes need to check if one string can be transformed into another by shifting characters clockwise in the alphabet. For example, "c" can be converted to "e" using 2 clockwise shifts (c → d → e). In this problem, we have two strings p and q, and a number r representing the maximum allowed shifts. We need to determine if p can be converted to q by shifting characters clockwise at most r times in total. Example If p = "abc", q = "ccc", and r = 3, the answer is True ...
Read MoreProgram to add two numbers represented as strings in Python
Suppose we have two strings S and T, these two are representing integers. We have to add them and find the result in the same string representation. So, if the input is like "256478921657", "5871257468", then the output will be "262350179125", as 256478921657 + 5871257468 = 262350179125. Approach To solve this, we will follow these steps − Convert S and T from string to integer Add the integers: result = S + T Return the result as string Example Let us see the following implementation to get better understanding − ...
Read MoreProgram to check whether list is strictly increasing or strictly decreasing in Python
Suppose we have a list of numbers; we have to check whether the list is strictly increasing or strictly decreasing. A list is strictly increasing if each element is larger than the previous one, and strictly decreasing if each element is smaller than the previous one. So, if the input is like nums = [10, 12, 23, 34, 55], then the output will be True, as all elements are distinct and each element is larger than the previous one, so this is strictly increasing. Algorithm To solve this, we will follow these steps ? If size of nums
Read MoreProgram to sqeeze list elements from left or right to make it single element in Python
Suppose we have a list of numbers called nums, we have to squeeze it from both the left and the right until there is one remaining element. We will return the states at each step. So, if the input is like nums = [10, 20, 30, 40, 50, 60], then the output will be ? [ [10, 20, 30, 40, 50, 60], [30, 30, 40, 110], [60, 150], [210] ] Algorithm To solve this, we will follow these steps ? ret := a list with only ...
Read MoreProgram to sorting important mails from different mailboxes in Python
Suppose we have a list of mailboxes where each mailbox contains a list of strings. Each string represents either "J" for junk, "P" for personal, or "W" for work emails. We need to process each mailbox in round-robin order starting from the first mailbox, filter out junk emails, and return a single sorted list. So, if the input is like mailboxes = [["W", "P"], ["J", "P", "J"], ["W"]], then the output will be ["W", "W", "P", "P"]. In processing order without filtering, we have W → J → W → P → P → J. After filtering out junk ...
Read MoreProgram to count number of elements are placed at correct position in Python
Suppose we have a list of numbers called nums, we have to find the number of elements that are present in the correct indices, when the list was to be sorted. So, if the input is like [2, 8, 4, 5, 11], then the output will be 2, as the elements 2 and 11 are in their correct positions. The sorted sequence will be [2, 4, 5, 8, 11] Algorithm To solve this, we will follow these steps − s := sort the list nums count := 0 for i in range 0 to size ...
Read MoreProgram to check whether we can stand at least k distance away from the closest contacts in Python
Suppose we have a string s and a number k. Each character in the string is either a dot ('.') or 'x', where dot indicates an empty space and 'x' indicates a person. We need to check whether it's possible to choose a position to stand on such that the distance between us and the closest person is at least k units away. The distance between neighboring indices is 1. For example, if the input is s = "x...x.." and k = 2, the output will be True because we can stand at position s[2] or s[6]. Algorithm ...
Read MoreProgram to find shortest sublist so after sorting that entire list will be sorted in Python
Sometimes we need to find the shortest sublist that, when sorted, makes the entire list sorted. This problem involves comparing the original list with its sorted version to identify the boundaries of the unsorted region. So, if the input is like nums = [1, 2, 5, 4, 9, 10], then the output will be 2, as sorting the sublist [5, 4] would make the entire list sorted: [1, 2, 4, 5, 9, 10]. Algorithm To solve this, we will follow these steps − Initialize first := -1, last := -1 to track the boundaries Create ...
Read More