You are on page 1of 2

DEPARTMENT OF COMPUTER APPLICATIONS

PSG COLLEGE OF TECHNOLOGY

Problemsheet -2 - Strings

1. You are given a string and your task is to swap cases. In other words, convert all lowercase letters to
uppercase letters and vice versa. eg: Psg Tech  pSG tECH , Pythonist 2 → pYTHONIST 2

2. You are given the firstname and lastname of a person on two different lines. Your task is to read them
and print the following: Hello firstname lastname! Good You love python coding.

3. Accept a string and a substring. You have to print the number of times that the substring occurs in
the given string. eg: ACACACAAC – substring AC occurs 4 times

4. You need to type a cheque using your keyboard. Your keyboard has a broken key for numeric 6.
so if the amount contains the number 6 , two cheques have to be typed which equals the original
amount. For eg input is 46—output can be 23+23 or 43+3 ( which doesnot contain the number
6).Many output combination are possible ,you can write a solution for any one combination.

5. Given two strings which are of lengths n and n+1, The second string contains all the character of the
first string, but there is one extra character. Your task to find the extra character in the second string.
Input : string strA = "abcd";
string strB = "cbdae";
Output : e : explanation:string B contain all the element there is a one extra character which is e

Input : string strA = "kxml";


string strB = "klxml";
Output : l : explanationstring B contain all the element there is a one extra character which is l

6. Reverse words in a given string


input : ‘this is pretty’
output: ‘siht si ytterp’

7. Print all Permutations of a given string


Input: ABC OUTPUT: ABC ACB BAC BCA CAB CBA
Input: ABSG Output:
ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB
GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA

8. Longest Palindrome in a String : Given a string S, find the longest palindromic substring in
S.Substring of string S: S[ i . . . . j ] where 0 ≤ i ≤ j < len(S). Palindrome string: A string which
reads the same backwards. More formally, S is palindrome if reverse(S) = S. Incase of conflict,
return the substring which occurs first ( with the least starting index ).
Input: Input: aaaabbaa output: aabbaa

9. Recursively remove all adjacent duplicates: Given a string s, recursively remove adjacent duplicate
characters from the string s. The output string should not have any adjacent duplicates.
input: azxxzy Output: ay. Explanation: First “azxxzy” is reduced to “azzy”. The string “azzy”
contains duplicates,so it is further reduced to “ay”.
Input: caaabbbaacdddd Output: Empty String
Input: acaaabbbacdddd Output: acac
10. Roman Number to Integer : Converting Roman Numerals to Decimal lying between 1 to 3999.
Input : IX Output : 9
Input : XL Output : 40
Input : MCMIV Output : 1904 . M is a thousand, CM is nine hundred
and IV is four

11. Longest Distinct Characters in the string : Given a string S, find length of the longest substring with
all distinct characters. For example, for input "abca", the output is 3 as "abc" is the longest substring
with all distinct characters. Print length of smallest substring with maximum number of distinct
characters. Note: The output substring should have all distinct characters.
input: abababcdefababcdab output : 6 ( for string abcdef)

12. Longest Common Prefix: Given a set of strings, find the longest common prefix.
Input : { “geeks”, “geek”, “geezer”}
Output : "gee"

Input : {"apple", "ape", "april"}


Output : "ap"

( You can accept three strings for practice initially. Later try with list of words)

You might also like