- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find number of different integers in a string using Python
Suppose we have a lowercase alphanumeric string s. We shave to replace every non-digit character with a space, but now we are left with some integers that are separated by at least one space. We have to find the number of different integers after performing the replacement operations on s. Here two numbers are considered as different if their decimal representations without any leading zeros are different.
So, if the input is like s = "ab12fg012th5er67", then the output will be 3 because, there are few numbers ["12", "012", "5", "67"] now "12" and "012" are different in string but same as integer. So there are three distinct numbers.
To solve this, we will follow these steps −
nums := a new list
k := blank string
for i in range 0 to size of s, do
if ASCII of s[i] > 47 and ASCII of s[i] < 58, then
k := k concatenate s[i]
otherwise,
if k is not a blank string, then
insert integer form of k at the end of nums
k := blank string
if k is not a blank string, then
insert integer form of k at the end of nums
return count of distinct elements in nums
Let us see the following implementation to get better understanding −
Example
def solve(s): nums = [] k = "" for i in range(len(s)): if ord(s[i]) > 47 and ord(s[i]) < 58: k += s[i] else: if(k != ""): nums.append(int(k)) k = "" if(k != ""): nums.append(int(k)) return len(set(nums)) s = "ab12fg012th5er67" print(solve(s))
Input
"ab12fg012th5er67"
Output
3
- Related Articles
- Program to find number of different substrings of a string for different queries in Python
- Program to find least number of unique integers after K removals using Python
- Program to find number of good ways to split a string using Python
- Program to find the number of unique integers in a sorted list in Python
- Program to find number of different subsequences GCDs in Python
- Different Methods to find Prime Number in Python Program
- Program to find number of ways to split a string in Python
- Analysis of Different Methods to find Prime Number in Python program
- Python program to count number of vowels using set in a given string
- Program to find minimum number of monotonous string groups in Python
- Program to find out number of distinct substrings in a given string in python
- Python program to count the number of vowels using set in a given string
- Python program to count the number of vowels using sets in a given string
- Program to find shortest string after removing different adjacent bits in Python
- Program to find maximum number of balls in a box using Python
