For a given string S, comprising of only lowercase English alphabets, eliminate the vowels from the string that occur between two consonants(sandwiched between two immediately adjacent consonants). Print the updated string on a new line.
Example 1:
Input : S = "bab"
Output : bb
Explanation:
a is a vowel occuring between two consonants i.e. b. 
Hence the updated string eliminates a.
Example 2:
Input : S = "ceghij"
Output : cghj
 
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function Sandwiched_Vowel() that takes a String (s), and return the updated String. The driver code takes care of the printing.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
 
Constraints:
1 ≤ L ≤ 10^{5}, where L is length of string S
'a' ≤ S[i] ≤ 'z', i is an integer denoting index of S

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



  

def Sandwiched_Vowel(s):

    #Complete the function

 

Solutions:
def Sandwiched_Vowel(S):
	vowels = 'aeiou'
	updated_string = ''
	n = len(S)
	for i in range(n):
		if S[i] in vowels:
			if i > 0 and i < n - 1 and (S[i - 1] not in vowels) and (S[i + 1] not in vowels):
				continue
		updated_string += S[i]
	return updated_string


def Sandwiched_Vowel(s):
	a = s
	vowels = ['a', 'e', 'i', 'o', 'u']
	i = 0
	range = len(a)
	while i < range:
		if a[i] in vowels:
			if i == 0 or i == len(a) - 1:
				i += 1
				continue
			if a[i - 1] not in vowels and a[i + 1] not in vowels:
				a = a[:i] + a[i + 1:]
				range = len(a)
		i += 1
	return a


def Sandwiched_Vowel(s):
	i = 1
	vowel = ['a', 'e', 'i', 'o', 'u']
	consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']
	while i < len(s) - 1:
		if s[i] in vowel:
			if s[i - 1] in consonants and s[i + 1] in consonants:
				s = s[0:i] + s[i + 1:]
			else:
				i += 1
		else:
			i += 1
	return s


def Sandwiched_Vowel(s):
	vowels = 'aeiou'
	res = ''
	for i in range(len(s)):
		if s[i] not in vowels:
			res += s[i]
		elif i > 0 and i < len(s) - 1 and (s[i - 1] not in vowels) and (s[i + 1] not in vowels):
			continue
		else:
			res += s[i]
	return res


def Sandwiched_Vowel(s):
	work = [ele for ele in s]
	vowels = 'aeiou'
	result = []
	for i in range(len(work)):
		if work[i] not in vowels:
			result.append(work[i])
			continue
		elif i > 0 and i < len(work) - 1:
			if work[i - 1] not in vowels and work[i + 1] not in vowels:
				continue
		result.append(work[i])
	result = ''.join(result)
	return result


def Sandwiched_Vowel(s):
	i = 0
	v = 'aeiou'
	ans = ''
	while i < len(s):
		if i == 0 or i == len(s) - 1 or s[i] not in v:
			ans += s[i]
		elif s[i - 1] in v or s[i + 1] in v:
			ans += s[i]
		i += 1
	return ans


def Sandwiched_Vowel(s):
	a = ['a', 'e', 'i', 'o', 'u']
	s = list(s)
	l = s[:]
	for i in range(1, len(s) - 1):
		if s[i - 1] not in a and s[i] in a and (s[i + 1] not in a):
			l[i] = 0
	l = [x for x in l if x != 0]
	return ''.join(l)


def Sandwiched_Vowel(s):
	v = {'a', 'e', 'i', 'o', 'u'}
	v_idx = []
	for i in range(1, len(s) - 1, 1):
		if s[i] in v:
			v_idx.append(i)
	s = list(s)
	x = list(s)
	n = len(s)
	j = 0
	for i in v_idx:
		if i != 0 and i != n - 1:
			if s[i - 1] not in v and s[i + 1] not in v:
				x.pop(i - j)
				j += 1
	x = ''.join(x)
	return x


def Sandwiched_Vowel(s):
	vowels = 'aeiou'
	s = list(s)
	res = s[:]
	for i in range(1, len(s) - 1):
		if s[i] in vowels and s[i - 1] not in vowels and (s[i + 1] not in vowels):
			res[i] = 0
	res = [x for x in res if x != 0]
	return ''.join(res)


def Sandwiched_Vowel(s):
	vowels = 'aeiou'
	list_s = list(s)
	n = len(s)
	if n <= 2:
		return s
	i = 1
	while i < n - 1:
		if s[i - 1] not in vowels and s[i] in vowels and (s[i + 1] not in vowels):
			list_s[i] = ''
		i += 1
	return ''.join(list_s)


def Sandwiched_Vowel(s):
	if len(s) <= 2:
		return s
	res = ''
	res += s[0]
	vow = ['a', 'e', 'i', 'o', 'u']
	for i in range(1, len(s) - 1):
		if s[i] in vow and s[i - 1] not in vow and (s[i + 1] not in vow):
			continue
		res += s[i]
	res += s[-1]
	return res


def Sandwiched_Vowel(s):
	vowel = 'aeiou'
	a = ''
	if len(s) == 1:
		return s
	a += s[0]
	for index in range(1, len(s) - 1):
		if s[index] not in vowel:
			a += s[index]
		elif s[index - 1] in vowel or s[index + 1] in vowel:
			a += s[index]
	a += s[-1]
	return a


def Sandwiched_Vowel(s):
	vowels = 'a,e,i,o,u'
	result = ''
	for i in range(len(s)):
		if s[i] not in vowels:
			result += s[i]
		elif i > 0 and i < len(s) - 1 and (s[i + 1] not in vowels) and (s[i - 1] not in vowels):
			continue
		else:
			result += s[i]
	return result


def Sandwiched_Vowel(string):
	newstr = ''
	l = []
	vowels = ['a', 'e', 'i', 'o', 'u']
	for i in range(len(string)):
		if string[i] not in vowels:
			l.append(i)
	newstr = string
	for i in range(len(l)):
		if i < len(l) - 1:
			if l[i + 1] - l[i] == 2:
				newstr = newstr[0:l[i] + 1 - (len(string) - len(newstr))] + newstr[l[i] + 1 - (len(string) - len(newstr)) + 1:]
	return newstr


def Sandwiched_Vowel(string):
	newstr = ''
	l = []
	vowels = ['a', 'e', 'i', 'o', 'u']
	i = 0
	while i < len(string):
		if i < len(string) - 2 and string[i + 1] in vowels and (string[i + 2] not in vowels) and (string[i] not in vowels):
			newstr = newstr + string[i]
			i = i + 2
		else:
			newstr = newstr + string[i]
			i = i + 1
	return newstr


def Sandwiched_Vowel(s):
	vowels = 'aeiou'
	new_s = ''
	for i in range(len(s)):
		if s[i] not in vowels:
			new_s += s[i]
		elif i == 0 or i == len(s) - 1 or s[i - 1] in vowels or (s[i + 1] in vowels):
			new_s += s[i]
	return new_s


def Sandwiched_Vowel(s):
	v = 'aeiou'
	ans = ''
	for i in range(len(s)):
		if s[i] not in v:
			ans += s[i]
		elif i > 0 and i < len(s) - 1 and (s[i - 1] not in v) and (s[i + 1] not in v):
			continue
		else:
			ans += s[i]
	return ans


def Sandwiched_Vowel(s):
	S = ''
	if len(s) <= 2:
		return s
	vowel = ['a', 'e', 'i', 'o', 'u']
	S += s[0]
	for i in range(1, len(s) - 1):
		if s[i] not in vowel:
			S += s[i]
		elif s[i - 1] not in vowel and s[i + 1] not in vowel:
			continue
		else:
			S += s[i]
	S += s[-1]
	return S


def Sandwiched_Vowel(s):
	v = ['a', 'e', 'i', 'o', 'u']
	if len(s) == 1:
		return s[0]
	l = s[0]
	for i in range(1, len(s) - 1):
		if s[i] in v and s[i - 1] not in v and (s[i + 1] not in v):
			pass
		else:
			l += s[i]
	l += s[len(s) - 1]
	return l


import re

def Sandwiched_Vowel(s):
	if len(s) < 3:
		return s
	vrr = 'aeiou'
	t = s[0]
	for i in range(1, len(s) - 1):
		if s[i] in vrr and s[i - 1] not in vrr and (s[i + 1] not in vrr):
			pass
		else:
			t = t + s[i]
	return t + s[-1]


def Sandwiched_Vowel(s):
	if len(s) <= 2:
		return s
	vowels = ['a', 'e', 'i', 'o', 'u']
	for i in range(1, len(s) - 1):
		if i >= len(s) - 1:
			break
		elif s[i] not in vowels:
			pass
		elif s[i - 1] in vowels:
			pass
		elif s[i + 1] in vowels:
			pass
		else:
			s = s[:i] + s[i + 1:]
			i = i - 1
	return s


def Sandwiched_Vowel(s):
	w = ''
	for i in range(len(s)):
		if s[i] not in 'aeiou':
			w += s[i]
		elif i == 0 or i == len(s) - 1:
			w += s[i]
		elif s[i - 1] in 'aeiou' or s[i + 1] in 'aeiou':
			w += s[i]
	return w


def Sandwiched_Vowel(s):
	if len(s) == 1:
		return s
	s2 = s[0]
	set1 = set('aeiou')
	for i in range(1, len(s) - 1):
		if s[i] in set1 and s[i - 1] not in set1 and (s[i + 1] not in set1):
			pass
		else:
			s2 += s[i]
	s2 += s[len(s) - 1]
	return s2


def Sandwiched_Vowel(s):
	l = ''
	if len(s) > 1:
		l += s[0]
	for i in range(1, len(s) - 1):
		if s[i] in 'aeiou' and s[i - 1] not in 'aeiou' and (s[i + 1] not in 'aeiou'):
			pass
		else:
			l += s[i]
	l += s[len(s) - 1]
	return l


def isVowel(x):
	if x == 'a' or x == 'e' or x == 'i' or (x == 'o') or (x == 'u'):
		return True
	else:
		return False

def Sandwiched_Vowel(a):
	n = len(a)
	updatedString = ''
	for i in range(0, n, 1):
		if i == 0 or i == n - 1:
			updatedString += a[i]
			continue
		if isVowel(a[i]) == True and isVowel(a[i - 1]) == False and (isVowel(a[i + 1]) == False):
			continue
		updatedString += a[i]
	return updatedString


def Sandwiched_Vowel(s):
	l = set(['a', 'e', 'i', 'o', 'u'])
	l1 = [j for j in s[1:-1]]
	r = set([k for k in range(1, len(s) - 1) if s[k - 1] not in l and s[k + 1] not in l and (s[k] in l)])
	r1 = ''
	for j in range(len(s)):
		if j not in r:
			r1 = r1 + s[j]
	return r1


def Sandwiched_Vowel(s):
	if len(s) == 1:
		return s
	vowels = 'aeiou'
	result = s[0]
	for i in range(1, len(s) - 1):
		if s[i] not in vowels or s[i - 1] in vowels or s[i + 1] in vowels:
			result += s[i]
	return result + s[-1]


def Sandwiched_Vowel(s):
	a = list(s)
	n = len(a)
	vowels = ['a', 'e', 'i', 'o', 'u']
	res = ''
	for i in range(1, n - 1):
		if a[i] in vowels and a[i - 1] not in vowels and (a[i + 1] not in vowels):
			a[i] = '#'
	for i in range(n):
		if a[i] != '#':
			res += a[i]
	return res


def is_vowel(x):
	if x == 'a' or x == 'e' or x == 'i' or (x == 'o') or (x == 'u'):
		return True
	else:
		return False

def Sandwiched_Vowel(n):
	l = len(n)
	s = ''
	for i in range(0, l):
		if i == 0 or i == l - 1:
			s += n[i]
			continue
		if is_vowel(n[i]) == True and is_vowel(n[i - 1]) == False and (is_vowel(n[i + 1]) == False):
			continue
		s += n[i]
	return s


def Sandwiched_Vowel(s):
	string = s[:]
	s = string[0]
	vowels = ('a', 'e', 'i', 'o', 'u')
	for i in range(1, len(string) - 1):
		if string[i - 1] not in vowels and string[i] in vowels and (string[i + 1] not in vowels):
			s += ''
		else:
			s += string[i]
	if len(string) > 1:
		s += string[-1]
	return s


def Sandwiched_Vowel(s):
	if len(s) < 3:
		return s
	res = s[0]
	N = len(s)
	for i in range(1, N - 1):
		if s[i] in 'aeiou' and s[i - 1] not in 'aeiou' and (s[i + 1] not in 'aeiou'):
			pass
		else:
			res += s[i]
	return res + s[-1]


def Sandwiched_Vowel(s):
	if len(s) == 1:
		return s
	res = str(s[0])
	K = ['a', 'e', 'i', 'o', 'u']
	for i in range(1, len(s) - 1):
		if s[i] in K and s[i - 1] not in K and (s[i + 1] not in K):
			pass
		else:
			res += s[i]
	res += str(s[-1])
	return res


def Sandwiched_Vowel(s):
	a = 'aeiou'
	b = ''
	for i in range(len(s)):
		if s[i] not in a:
			b += s[i]
		elif i > 0 and i < len(s) - 1 and (s[i - 1] not in a) and (s[i + 1] not in a):
			continue
		else:
			b += s[i]
	return b


def Sandwiched_Vowel(s):
	if len(s) == 1:
		return s
	s = list(s)
	i = 1
	n = ['a', 'e', 'i', 'o', 'u']
	while len(s) - 1 != i:
		if s[i - 1] not in n and s[i] in n and (s[i + 1] not in n):
			s.pop(i)
		else:
			i += 1
	return ''.join(s)


def check(c):
	if c == 'a' or c == 'e' or c == 'i' or (c == 'o') or (c == 'u'):
		return True
	return False

def Sandwiched_Vowel(s):
	temp = ''
	temp += s[0]
	for i in range(1, len(s) - 1):
		if check(s[i]) == True and check(s[i - 1]) == False and (check(s[i + 1]) == False):
			continue
		else:
			temp += s[i]
	if len(s) != 1:
		temp += s[len(s) - 1]
	return temp


def Sandwiched_Vowel(s):
	updated_string = ''
	for i in range(len(s)):
		if s[i] in 'aeiou':
			if i > 0 and i < len(s) - 1 and (s[i - 1] not in 'aeiou') and (s[i + 1] not in 'aeiou'):
				continue
		updated_string += s[i]
	return updated_string


def Sandwiched_Vowel(s):
	a = ['a', 'e', 'i', 'o', 'u']
	b = ''
	l = [x for x in s]
	if len(s) == 3 and s[0] not in a and (s[1] in a) and (s[2] not in a):
		s = s.replace(s[1], '0')
	for i in range(1, len(s) - 1):
		if s[i - 1] not in a and s[i] in a and (s[i + 1] not in a):
			l[i] = '0'
	for i in l:
		b += i
	b = b.replace('0', '')
	return b


def Sandwiched_Vowel(s):
	s = list(s)
	s1 = list('aeiou')
	for i in range(1, len(s) - 1):
		if s[i] in s1:
			if s[i - 1] not in s1:
				if s[i + 1] not in s1:
					s[i] = ''
	return ''.join(s)


def Sandwiched_Vowel(s):
	vowel = ['a', 'e', 'i', 'o', 'u']
	ans = s[0]
	if len(s) > 1:
		for i in range(1, len(s) - 1):
			if s[i] in vowel and s[i - 1] not in vowel and (s[i + 1] not in vowel):
				ans += ''
			else:
				ans += s[i]
		return ans + s[-1]
	else:
		return ans


def Sandwiched_Vowel(s):
	if len(s) < 3:
		return s
	s = str(s)
	new_s = s[0]
	vowel = 'aeiou'
	for i in range(1, len(s) - 1):
		if s[i] in vowel and s[i - 1] not in vowel and (s[i + 1] not in vowel):
			continue
		else:
			new_s += s[i]
	return new_s + s[-1]


def Sandwiched_Vowel(s):
	vow = 'aeoui'
	a = list(s)
	for i in range(1, len(s) - 1):
		if s[i - 1] not in vow and s[i + 1] not in vow and (s[i] in vow):
			a[i] = 0
	a = [x for x in a if x != 0]
	return ''.join(a)


def Sandwiched_Vowel(s):
	result = list(s)
	for i in range(1, len(s) - 1):
		if s[i] in 'aeiou' and s[i - 1] not in 'aeiou' and (s[i + 1] not in 'aeiou'):
			result[i] = 0
	return ''.join([i for i in result if i != 0])


def Sandwiched_Vowel(s):
	l = list(s)
	v = {'a', 'e', 'i', 'o', 'u'}
	for i in range(1, len(l) - 1):
		if l[i] in v and l[i - 1] not in v and (l[i + 1] not in v):
			l[i] = '$'
	res = ''
	for i in l:
		if i != '$':
			res += i
	return res


def Sandwiched_Vowel(s):
	k = ''
	q = 'aeiou'
	a = list(s)
	for i in range(1, len(s) - 1):
		if s[i] in q and s[i - 1] not in q and (s[i + 1] not in q):
			a[i] = 0
	for i in a:
		if i != 0:
			k = k + i
	return k


def Sandwiched_Vowel(s):
	k = ''
	l = 'aeiou'
	for i in range(len(s)):
		if s[i] not in l:
			k += s[i]
		elif i > 0 and i < len(s) - 1 and (s[i - 1] not in l) and (s[i + 1] not in l):
			continue
		else:
			k += s[i]
	return k


def Sandwiched_Vowel(s):
	l = ['a', 'e', 'i', 'o', 'u']
	l1 = list(s)
	c = 0
	for i in range(1, len(s) - 1):
		if s[i - 1] not in l and s[i] in l and (s[i + 1] not in l):
			l1.pop(i - c)
			c += 1
	return ''.join(l1)


def Sandwiched_Vowel(s):
	res = ''
	for i in range(len(s)):
		if i == 0 or i == len(s) - 1:
			if s[i] in 'aeiou':
				res += s[i]
			else:
				res += s[i]
		elif s[i] in 'aeiou':
			if not s[i - 1] in 'aeiou' and (not s[i + 1] in 'aeiou'):
				pass
			else:
				res += s[i]
		else:
			res += s[i]
	return res


def Sandwiched_Vowel(s):
	vowel = ['a', 'e', 'i', 'o', 'u']
	s = list(s)
	n = len(s)
	s1 = s + []
	for i in range(1, n - 1):
		if s[i] in vowel and s[i - 1] not in vowel and (s[i + 1] not in vowel):
			s1[i] = '1'
	x = ''
	for i in range(len(s1)):
		if s1[i] != '1':
			x += s1[i]
	return x


def Sandwiched_Vowel(s):
	A = 'aeiou'
	B = ''
	for i in range(len(s)):
		if s[i] not in A:
			B = B + s[i]
		elif i > 0 and i < len(s) - 1 and (s[i - 1] not in A) and (s[i + 1] not in A):
			continue
		else:
			B = B + s[i]
	return B


def Sandwiched_Vowel(s):
	i = 1
	v = ['a', 'e', 'i', 'o', 'u']
	ns = ''
	ns += s[0]
	while i < len(s) - 1:
		if s[i] in v:
			if s[i - 1] not in v and s[i + 1] not in v:
				pass
			else:
				ns += s[i]
		else:
			ns += s[i]
		i += 1
	if len(s) > 1:
		ns += s[-1]
	return ns


def Sandwiched_Vowel(s):
	i = 1
	while i < len(s) - 1:
		if s[i] in 'aeiou' and s[i - 1] not in 'aeiou' and (s[i + 1] not in 'aeiou'):
			s = s[0:i] + s[i + 1:]
			i += 1
		else:
			i += 1
	return s


def Sandwiched_Vowel(s):
	n = len(s) - 1
	j = 1
	r = s[0]
	v = {'a', 'e', 'i', 'o', 'u'}
	if n < 2:
		return s
	while j < n:
		if s[j] in v and s[j - 1] not in v and (s[j + 1] not in v):
			j += 1
			continue
		r += s[j]
		j += 1
	return r + s[-1]


def Sandwiched_Vowel(s):
	ans = '' + s[0]
	n = len(s)
	if n == 1:
		return s
	vowels = ['a', 'e', 'i', 'o', 'u']
	for i in range(1, len(s) - 1):
		if s[i] in vowels and s[i - 1] not in vowels and (s[i + 1] not in vowels):
			continue
		else:
			ans += s[i]
	else:
		ans += s[-1]
	return ans


def Sandwiched_Vowel(s):
	vov = 'aeiou'
	ans = ''
	l = len(s)
	for x in range(l):
		if s[x] not in vov:
			ans += s[x]
		elif x > 0 and x < l - 1 and (s[x - 1] not in vov) and (s[x + 1] not in vov):
			continue
		else:
			ans += s[x]
	return ans