
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the character in first string that is present at minimum index in second string in Python
Suppose we have a string str and another string patt, we have to find determine the character in patt that is present at the minimum index of str. If no character patt1 is present in str1 then return -1.
So, if the input is like str = "helloworld" and patt = "wor", then the output will be 'o' as 'o' is present at minimum index in str
To solve this, we will follow these steps −
for i in range 0 to size of patt, do
for j in range 0 to size of Str, do
if patt[i] is same as Str[j] and j < minimum_index, then
minimum_index := j
come out from the loop
if minimum_index is not same as 10^9 , then
return Str[minimum_index]
otherwise,
return -1
Example
Let us see the following implementation to get better understanding −
def get_min_index_char(Str, patt): minimum_index = 10**9 for i in range(len(patt)): for j in range(len(Str)): if (patt[i] == Str[j] and j < minimum_index): minimum_index = j break if (minimum_index != 10**9): return Str[minimum_index] else: return -1 Str = "helloworld" patt = "wor" print(get_min_index_char(Str, patt))
Input
"helloworld", "wor"
Output
o
- Related Questions & Answers
- Find repeated character present first in a string in C++
- Program to find the index of first Recurring Character in the given string in Python
- Return the index of first character that appears twice in a string in JavaScript
- Decoded String at Index in Python
- How to concatenate two strings so that the second string must concatenate at the end of first string in JavaScript?
- Find the index of the first unique character in a given string using C++
- Is the second string a rotated version of the first string JavaScript
- Python Program that Displays which Letters are in the First String but not in the Second
- Return index of first repeating character in a string - JavaScript
- Finding the index of the first repeating character in a string in JavaScript
- First Unique Character in a String in Python
- Find all types of date format that present in the string using Python
- Find the first repeated character in a string using C++.
- Check whether the Average Character of the String is present or not in Python
- Second most frequent character in a string - JavaScript
Advertisements