Skip to content

Latest commit

 

History

History
267 lines (233 loc) · 5.46 KB

String Practice Questions.md

File metadata and controls

267 lines (233 loc) · 5.46 KB
1. Reverse the order of words present in the string
def reverse_sentence(s):
	word=""
	words=[]
	for char in s:
		if char==' ':
			if word:
				words.append(word)
				word=""
		else:
			word=word+char
	if word:
		words.append(word)
	reverse_sentence=""
	for i in range(len(words)-1,-1,-1):
		reverse_sentence+=words[i]
		if i>0:
			reverse_sentence=reverse_sentence+" "
	return reverse_sentence

s="hello world hey"
print(reverse_sentence(s))

2. Check Anagram of the two string
def are_anagrams(s1, s2):
    if len(s1) != len(s2):
        return False

    count1 = [0] * 256
    count2 = [0] * 256

    for i in range(len(s1)):
        count1[ord(s1[i])] += 1
        count2[ord(s2[i])] += 1

    for i in range(256):
        if count1[i] != count2[i]:
            return False
    return True

s1 = "listen"
s2 = "silent"
print(are_anagrams(s1, s2))

3. The given Two strings are isomorphic string or not
def isIsomorphic(s, t):
    mapST = {}
    mapTS = {}
    
    for i in range(len(s)):
        c1, c2 = s[i], t[i]
        if ((c1 in mapST and mapST[c1] != c2) or (c2 in mapTS and mapTS[c2] != c1)):
            return False
        mapST[c1] = c2
        mapTS[c2] = c1
    
    return True

s1 = "egg"
t1 = "add"
print(isIsomorphic(s1, t1))

s2 = "foo"
t2 = "bar"
print(isIsomorphic(s2, t2))

4. Check the output string is the rotation of the given string
def rotateString(s, goal):
    if len(s) != len(goal):
        return False
    concatenated_string = s + s
    for i in range(len(concatenated_string) - len(goal) + 1):
        if concatenated_string[i:i + len(goal)] == goal:
            return True
    return False

s = "abcde"
goal = "cdeab"
print(rotateString(s, goal))

s = "abcde"
goal = "abced"
print(rotateString(s, goal))

5. Maximum depth of the nesting parenthesis in the given string
def maxDepth(s):
    max_count = 0
    count = 0
    for i in range(len(s)):
        if s[i] == "(":
            count = count + 1
        elif s[i] == ")":
            count = count - 1
        if count > max_count:
            max_count = count
    return max_count

s = "(1+(2*3)+((8)/4))+1"
print(maxDepth(s))

s = "()(())((()()))"
print(maxDepth(s))

6. Find the largest odd number in the given string :
def largestOddNumber(num):
    for i in range(len(num) - 1, -1, -1):
        if int(num[i]) % 2 != 0:
            return num[:i + 1]
    return ""

num = "35427"
print(largestOddNumber(num))

num = "4206"
print(largestOddNumber(num))

7. Check the given substring is present in the available string
def contains_substring(s, substr):
    for i in range(len(s) - len(substr) + 1):
        if s[i:i + len(substr)] == substr:
            return True
    return False

s = "hello world"
substr = "world"
print(contains_substring(s, substr))

8. Remove the Outermost Parenthesis in the given string
def removeOuterParentheses(s):
    count = 0
    ans = ""
    for i in range(len(s)):
        if s[i] == '(':
            count = count + 1
        else:
            count = count - 1
        if count == 0:
            continue
        elif (count == 1 and s[i] == '('):
            continue
        ans = ans + s[i]
    return ans

s = "(()())(())"
print(removeOuterParentheses(s))

s = "()()"
print(removeOuterParentheses(s))

9.Longest common prefix in the given strings
def longestCommonPrefix(strs):
    result = ""
    for i in range(len(strs[0])):
        for s in strs:
            if i == len(s) or s[i] != strs[0][i]:
                return result
        result = result + strs[0][i]
    return result

strs = ["flower","flow","flight"]
print(longestCommonPrefix(strs))

strs = ["dog","racecar","car"]
print(longestCommonPrefix(strs))

10. Two strings Comparison
def compare_strings(s1, s2):
    if len(s1) != len(s2):
        return False
    
    for i in range(len(s1)):
        if s1[i] != s2[i]:
            return False
            
    return True

s1 = "hello"
s2 = "hello"
print(compare_strings(s1, s2))

s1 = "hello"
s2 = "world"
print(compare_strings(s1, s2))

11. Return the First Non Repeating Character in the given string
def first_non_repeating_character(s):
    char_count = {}
    
    for char in s:
        char_count[char] = char_count.get(char, 0) + 1
    
    for char in s:
        if char_count[char] == 1:
            return char
    
    return None

s = "swiss"
print(first_non_repeating_character(s))

s = "leetcode"
print(first_non_repeating_character(s))

12. Count how many Substrings can be produced from the given string
# Check the length of the strings
# And apply this formula to check how many strings n * (n + 1) // 2

13. Check the given string is palindrome or not
def is_palindrome(s):
    n = len(s)
    for i in range(n // 2):
        if s[i] != s[n - i - 1]:
            return False
    return True

s = "madam"
print(is_palindrome(s))

s = "racecar"
print(is_palindrome(s))

14. Take all the Substring from the given string and print
def substring_taken(s1):
    n = len(s1)
    substrings1 = []
    for i in range(n):
        strs = ""
        for j in range(i,n):
            strs = s1[i:j+1]
            substrings1.append(strs)
    return substrings1, len(substrings1)
    
string1 = "heisanartist"
Result, Length = substring_taken(string1)
print(Result)
print(Length)