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
Server Side Programming Articles
Page 370 of 2109
Program to check whether one string swap can make strings equal or not using Python
Given two strings of equal length, we need to determine if we can make them identical by performing at most one swap operation on exactly one of the strings. A swap operation exchanges two characters at any positions within a string. Problem Analysis For two strings to become equal with at most one swap, they must satisfy these conditions: If strings are already identical (0 differences) − return True If strings have exactly 2 differences − check if swapping makes them equal If strings have more than 2 differences − return False Both strings must contain ...
Read MoreProgram to check if binary string has at most one segment of ones or not using Python
Binary strings often need validation to check specific patterns. A common requirement is to verify if a binary string has at most one contiguous segment of ones. This means all the '1's should be grouped together without any '0's in between. For example, "111000" has one segment of ones, while "11010" has two separate segments. Problem Understanding Given a binary string without leading zeros, we need to determine if it contains at most one contiguous segment of ones. The string should have all '1's grouped together, followed by all '0's (if any). Examples "111000" ...
Read MoreProgram to find nearest point that has the same x or y coordinate using Python
Finding the nearest point that shares the same x or y coordinate is a common problem in computational geometry. We need to find a valid point (one that shares either x or y coordinate) with the smallest Manhattan distance from our current location. The Manhattan distance between two points (a, b) and (p, q) is calculated as |a - p| + |b - q|. When multiple points have the same minimum distance, we return the one with the smallest index. Problem Statement Given a list of points and a current location, find the index of the valid ...
Read MoreProgram to count items matching a rule using Python
Suppose we have an array nums, where each nums[i] contains three elements [type_i, color_i, name_i]. These are describing the type, color, and name of the ith item. We also have a rule represented by two other strings, ruleKey and ruleValue. Now we can say the ith item matches the rule if one of the following is true − ruleKey = "type" and ruleValue = type_i. ruleKey = "color" and ruleValue = color_i. ruleKey = "name" and ruleValue = name_i. We have to find the ...
Read MoreProgram to merge strings alternately using Python
Suppose we have two strings s and t. We have to merge them by adding letters in alternating fashion, starting from s. If s and t are not of same length, add the extra letters onto the end of the merged string. So, if the input is like s = "major" and t = "general", then the output will be "mgaejnoerral". Since t is larger than s, we have added extra part "ral" at the end. Algorithm To solve this, we will follow these steps − Initialize i := j := 0 ...
Read MoreProgram to find longest nice substring using Python
A nice substring is one where every letter appears in both uppercase and lowercase. For example, "bBb" is nice because 'b' appears as both 'b' and 'B'. This tutorial shows how to find the longest nice substring in Python. Problem Understanding Given a string, we need to find the longest substring where every letter appears in both uppercase and lowercase forms. If multiple substrings have the same maximum length, return the one that appears first. Algorithm Approach We'll use a nested loop approach to check all possible substrings: For each starting position, build a ...
Read MoreProgram to find largest perimeter triangle using Python
Suppose we have an array nums of positive lengths, we have to find the largest perimeter of a triangle by taking three values from that array. When it is impossible to form any triangle of non-zero area, then return 0. So, if the input is like [8, 3, 6, 4, 2, 5], then the output will be 19. Triangle Inequality Rule For three sides to form a triangle, they must satisfy the triangle inequality: the sum of any two sides must be greater than the third side. For sides a, b, c where a ≥ b ≥ ...
Read MoreProgram to find out the dot product of two sparse vectors in Python
A sparse vector is a vector where most elements are zero. The dot product of two vectors is calculated by multiplying corresponding elements and summing the results. For sparse vectors, we can optimize by skipping zero elements. Problem Statement Given two sparse vectors represented as lists, we need to calculate their dot product efficiently. The vectors are stored as objects with a nums attribute containing the list elements. Example If vector1 = [1, 0, 0, 0, 1] and vector2 = [0, 0, 0, 1, 1], the dot product is ? 1×0 + 0×0 + ...
Read MoreProgram to find out the number of boxes to be put into the godown in Python
Suppose we have two arrays containing integers. One list contains the height of some unit width boxes and another array contains the height of rooms in the godown. The rooms are numbered 0...n, and the height of the rooms is provided in their respective indexes in the array godown. We have to find out the number of boxes that can be pushed into the godown. A few things have to be kept in mind: The boxes can't be put one on another. The order of the boxes can be changed. ...
Read MoreProgram to find out if the strings supplied differ by a character in the same position in Python
Suppose, we are provided an array that contains several strings of the same length. We have to find out if any two of the supplied strings differ by a single character at the same position. If this difference is present, we return True, otherwise we return False. So, if the input is like words = ['pqrs', 'prqs', 'paqs'], then the output will be True. The output is True because all strings differ at index 1 ('q', 'r', 'a'), so any two pairs have a difference in the same position. Algorithm Steps To solve this, we will follow ...
Read More