Programming Articles

Page 474 of 2547

Check if a string has all characters with same frequency with one variation allowed in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 317 Views

Sometimes we need to check if a string can become "valid" by removing at most one character. A valid string means all unique characters have the same frequency. For example, "aab" is not valid (a appears 2 times, b appears 1 time), but we can remove one 'a' to make it "ab" where both characters appear once. Problem Example Given string s = "xyyzx", we can delete one 'z' to get "xyyx" where both 'x' and 'y' appear twice ? Approach The algorithm counts character frequencies and checks if we can achieve uniform frequency by removing ...

Read More

Check if a string follows a^n b^n pattern or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 312 Views

The a^n b^n pattern refers to a string that contains exactly n consecutive 'a' characters followed by exactly n consecutive 'b' characters. For example, when n = 3, the string will be "aaabbb". So, if the input is like s = "aaaaabbbbb", then the output will be True as this follows a^5 b^5 pattern. Algorithm To solve this, we will follow these steps − Get the size of the string Count consecutive 'a' characters from the beginning Check if the number of 'a's multiplied by 2 equals the total string length Verify that all remaining ...

Read More

Check if a string contains a palindromic sub-string of even length in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 457 Views

A palindrome is a string that reads the same forwards and backwards. In this problem, we need to check if a given string contains any palindromic substring of even length. The key insight is that the shortest even-length palindrome is of length 2, consisting of two identical adjacent characters (like "aa", "bb", etc.). Any longer even-length palindrome must contain at least one pair of adjacent identical characters. Algorithm To solve this problem efficiently, we can use this approach ? Iterate through the string from index 0 to length−2 For each position i, check if string[i] ...

Read More

Check if a string can become empty by recursively deleting a given sub-string in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 513 Views

Sometimes we need to check if a string can be completely emptied by repeatedly removing occurrences of a substring. This problem involves finding and removing a target substring until either the string becomes empty or no more occurrences exist. So, if the input is like s = "pipipinnn" t = "pin", then the output will be True as we can remove "pin" from "pipipinnn", then we will get "pipinn", again remove "pin" to get string "pin", then remove it to make it empty. Algorithm To solve this, we will follow these steps: while size of ...

Read More

Check if a string can be repeated to make another string in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 567 Views

When working with strings in Python, we sometimes need to check if one string can be repeated multiple times to form another string. This is useful in pattern matching and string manipulation tasks. Given two strings s and t, we need to find how many times string s must be concatenated to generate string t. If it's impossible to generate t using s, we return -1. Algorithm To solve this problem, we follow these steps ? Check if the length of t is divisible by the length of s If not divisible, return -1 (impossible ...

Read More

Check if a string can be obtained by rotating another string 2 places in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 379 Views

String rotation is a common programming problem where we check if one string can be obtained by rotating another string by a specific number of positions. In this case, we need to check if we can rotate a string exactly 2 places in either direction (left or right). Problem Understanding Given two strings s and t, we need to determine if string s can be obtained by rotating string t exactly 2 positions left or right. For example, if t = "takolka": Left rotation by 2: "takolka" → "kolkata" Right rotation by 2: "takolka" → "katakolka" ...

Read More

Check if a string can be converted to another string by replacing vowels and consonants in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 479 Views

Sometimes we need to check if one string can be transformed into another by replacing characters with specific rules. In this problem, we can only replace vowels with other vowels and consonants with other consonants. The transformation rules are: A vowel (a, e, i, o, u) can only be replaced with another vowel A consonant can only be replaced with another consonant Both strings must have the same length Example If we have s = "udpmva" and t = "itmmve", we can transform: u (vowel) → i (vowel) ✓ d (consonant) → t (consonant) ...

Read More

Check if a sorted array can be divided in pairs whose sum is k in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 235 Views

Suppose we have a sorted array of numbers and another number k. We need to check whether the given array can be divided into pairs such that the sum of every pair equals k. So, if the input is like arr = [1, 2, 3, 4, 5, 6] and k = 7, then the output will be True as we can form pairs like (1, 6), (2, 5) and (3, 4). Algorithm To solve this, we will follow these steps − Get the size of the array If the array size is odd, return False ...

Read More

Check if a queue can be sorted into another queue using a stack in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 425 Views

Sometimes we need to check if elements in a queue can be rearranged into sorted order using an auxiliary stack. This problem involves three data structures: an input queue, a stack for temporary storage, and an output queue for the final sorted sequence. Problem Understanding Given a queue with the first n natural numbers in unsorted order, we need to determine if we can sort them into non-decreasing sequence using these operations: Push or pop elements from stack Delete element from input queue Insert element into output queue For example, with queue [6, 1, ...

Read More

Check if a Queen can attack a given cell on chessboard in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 825 Views

In chess, a queen is the most powerful piece that can move horizontally, vertically, and diagonally any number of squares. Given two coordinates on a chessboard representing a queen's position and a target cell, we need to determine if the queen can attack that position. So, if the input is like Q = (1, 1) and O = (4, 4), then the output will be True as the queen can reach (4, 4) diagonally. ...

Read More
Showing 4731–4740 of 25,466 articles
« Prev 1 472 473 474 475 476 2547 Next »
Advertisements