String(represents an integer) to value

Write a function which converts the given string(which represents an integer) to its value. Example Input string : “3486” Output : 3486 Time complexity : O(n) Algorithm For positive integers, a. Initialize result = 0. b. Traverse the string. c. For every character in the string, result = result*10 + …

Read more

Lexicographic rank of string

Find the lexicographic rank of a string. (string without duplicates) Example Input string : “CAB” Output : Rank is 5 Total possible permutations are : ABC, ACB, BAC, BCA, CAB, CBA(lexicographic order) Therefore, rank is 5 Time complexity : O(n) Algorithm a. Initialize rank = 1. b. Traverse in the …

Read more

Remove spaces from a string

Given a string, write a function that will print the given string without spaces Example: INPUT : s  = “tu to rial cup” OUTPUT : “tutorialcup” Time Complexity: O(n) 1. Traverse the string with two indexes ie, i for traversing and j for storing chars in output string 2. If …

Read more

Sort an array of strings

Given an array of strings, write a function to sort them alphabetically Example INPUT : arr[] = {“bekz”,”bcdk”,”akleedf” } OUTPUT : “akleedf” , “bcdk”, “bekz” Here, in above example we can see first string will be “akleedf” as ‘a’ comes first in order. For the next two strings ‘b’ is …

Read more

Longest Palindromic Substring

Given a string, write a function that will find the longest palindromic substring Example INPUT  s = “ebcdcbn” OUTPUT “bcdcb” is the longest palindromic substring Method 1(Brute Force) : In this method we will pick all the substrings and check whether the substring is palindrom or not Time Complexity: O(n^3) …

Read more

Translate »