Binary Search Tree to Greater Sum Tree in C++

Ravi Ranjan
Updated on 04-Sep-2025 15:02:56

343 Views

In this article, we are given a binary search tree. Our task is to transform the given BST to a Greater Sum tree. A Greater Sum Tree with respect to the given BST is a tree where each node's value is replaced by the sum of all values greater than that node's value in the original BST. Below is an example scenarios to convert the given BST to a Greater Sum Tree: Example Scenario Input: BST inorder traversal= {0, 1, 2, 3, 4, 5, 6, 7, 8} Output: Greater Sum Tree = {36, 35, 33, 30, 26, 21, 15, ... Read More

Check if a String Has at Least One Letter and One Number in Python

Sarika Singh
Updated on 02-Sep-2025 13:23:14

5K+ Views

To check if a string contains at least one letter and one number in Python, you can - Use any() function with str.isalpha() function and str.isdigit() function to go through characters. Use re.search() function with appropriate regular expressions for pattern matching. Both methods are commonly used for validating input strings where letters and digits are required. Using any() Function with String Methods The any() function can be combined with str.isalpha() or str.isdigit() functions to check if at least one character in the string is a letter or a digit. This approach is used for partial checks within ... Read More

Verify String Contains Letters, Numbers, Underscores, and Dashes in Python

Sarika Singh
Updated on 02-Sep-2025 13:22:52

13K+ Views

To verify that a string contains only letters, numbers, underscores, and dashes in Python - Use re.fullmatch() function with a pattern like [A-Za-z0-9_-]+ for regex-based checking. Use set comparison with all() function for simple logic-based validation. Many systems restrict input to certain characters for security or formatting reasons. In this case, the allowed characters are alphabets (A–Z, a–z), digits (0–9), underscores (_), and hyphens (-). Using Regular Expressions The re module in Python allows you to define patterns to validate strings. You can use re.fullmatch() function to check if the entire string matches a specific pattern, such as ... Read More

Python Regular Expression to Check Alphanumeric String

Sarika Singh
Updated on 02-Sep-2025 13:22:21

6K+ Views

In this article, we focus on how to check if a string is alphanumeric using regular expressions in Python. Regular expressions are very useful for pattern matching and validation. To use them, first import the re library, which is included by default in Python. The regular expression ^[a-zA-Z0-9]+$ matches strings that contain only letters (both uppercase and lowercase) and numbers. This expression returns True if the string is alphanumeric; otherwise, it returns False. Using Regular Expressions By applying the re.match() function with the above pattern, you can check if the entire string contains only alphanumeric characters. Example: Checking an Alphanumeric ... Read More

Check If a String Contains Certain Characters in Python

Sarika Singh
Updated on 02-Sep-2025 13:21:55

9K+ Views

To check if a Python string contains only certain characters, you can use - set Comparison Regular Expressions Character Lists These approaches help you to verify whether every character in the string belongs to a defined set of allowed characters or not. Using Set Comparison You can create a set of allowed characters and check if all characters in the string belong to that set or not. If the string is a subset of the allowed set, it means the string contains only valid (accepted) characters. Example In the following example, we define a set of allowed numeric ... Read More

Check Unicode String for Numeric Characters in Python

Sarika Singh
Updated on 02-Sep-2025 13:21:31

852 Views

In Python, Unicode strings can contain numeric characters from various languages and scripts. To check if a Unicode string contains only numeric characters, we can use built-in string methods, regular expressions, or character iteration. These methods ensure that characters like Arabic numerals, Chinese digits, or superscripts are also recognized as valid numeric characters. Using isnumeric() Method The isnumeric() method returns True if all characters in the string are numeric, including Unicode numeric characters like ², ١, 一, etc. If any character is non-numeric or the string is empty, it returns False. Example: Unicode Numeric Characters In the following example, ... Read More

Getitem and Setitem in Python

Sarika Singh
Updated on 02-Sep-2025 13:16:24

4K+ Views

In Python, you can customize many operations like accessing or modifying elements of a list or dictionary using special methods. Two important methods that make your objects behave like lists or dictionaries are: __getitem__: called when you access an item using obj[key]. __setitem__: called when you assign a value using obj[key] = value. These are examples of dunder methods (short for "double underscore"). They are special methods in Python with names that begin and end with double underscores. When you use square bracket syntax on your object: obj[key] automatically triggers __getitem__ obj[key] = value automatically triggers __setitem__ ... Read More

Check If a String Has Alphabets or Numbers in Python

Sarika Singh
Updated on 02-Sep-2025 13:15:53

4K+ Views

In Python, you can check whether a string contains letters, numbers, or both using built-in string methods such as isalpha(), isdigit(), and isalnum(). You can also use loops or regular expressions for more customized checks. Checking for Only Alphabets The isalpha() method returns True if every character in the string is a letter (a–z or A–Z) and the string is not empty. It is useful for validating names or inputs that should contain only alphabets. Example In this example, we check if the string has only alphabets using the isalpha() method - text = "HelloWorld" print(text.isalpha()) The string returns ... Read More

Check if a Python String Contains Only Digits

Sarika Singh
Updated on 02-Sep-2025 13:15:29

11K+ Views

To check if a Python string contains only digits, you can use the built-in isdigit() method. This method returns True if all characters in the string are digits, and False otherwise. Besides isdigit(), you can also use loops or regular expressions to verify this condition. Using the isdigit() Method The isdigit() method is used to check if a string contains only digits. It returns True only when every character is a digit. Example In this example, we define a string that contains only digits and use the isdigit() method to check it. The output confirms that the string contains only ... Read More

Check If String Ends With Suffix in Python

Sarika Singh
Updated on 02-Sep-2025 13:15:11

2K+ Views

In Python, you can easily check whether a string or a substring ends with a specific suffix using the built-in endswith() method. This method returns True if the string ends with the specified suffix, otherwise it returns False. This method also allows to check multiple suffixes at once and can operate on substrings by specifying start and end positions. Using the endswith() Method The endswith() method in Python checks if a string ends with a specified suffix and returns True or False. It can also check for multiple suffixes when provided as a tuple. Following is the syntax of the ... Read More

1 2 3 4 5 ... 7806 Next
Advertisements