Decode String Problem

Problem Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer. You may assume that the input string is always valid; there are no extra white spaces, square brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there will not be input like 3a or 2[4]. ...

Remove duplicate characters in a string

Problem Given a string, Write a program to remove duplicate characters from the string. Examples Example 1: Input: s = kodeknight Output: kodenight Example 2: Input: s = k5kc Output: k5c Solution There are multiple approach to solve it. Method 1 : Brute Force Code C void removeDuplicates(char *str) { if (!str) { return; } int len = strlen(str); if (len < 2){ return; } int tail = 1; for (int i = 1; i < len; ++i) { int j; for (j = 0; j < tail; ++j) if (str[i] == str[j]) { break; } if (j == tail) { str[tail] = str[i]; ++tail; } } str[tail] = '\0'; } ...

Determine if a string has all unique characters

Problem Implement an algorithm to determine if a string has all unique characters. Solution Method 1 - Brute Force - Compare All Characters Brute force way can be we take each character in the string and compare it to every other character in the string. We do this using for loops. This would mean a time complexity of O(n^2) because of the nested loop. ...

Replace all spaces in c-style string with '%20'

Problem Write a C program that will replace all spaces with ‘%20′ Examples Input: s = "Mr John Smith" Output: "Mr%20John%20Smith Solution Method 1 - Count spaces and traverse from end The algorithm is as follows: Count the number of spaces during the first scan of the string. Parse the string again from the end and for each character: If a space is encountered, store “%20”. Else, store the character as it is in the newly shifted location. Code C void replaceSpaces(char[] str, int length) { int spaceCount = 0, newLength, i = 0; for (i = 0; i < length; i++) { if (str[i] == ' ') { spaceCount++; } } newLength = length + spaceCount * 2; str[newLength] = '\0'; for (i = length - 1; i >= 0; i--) { if (str[i] == '') { str[newLength - 1] = '0'; str[newLength - 2] = '2'; str[newLength - 3] = '%'; newLength = newLength - 3; } else { str[newLength - 1] = str[i]; newLength = newLength - 1; } } } ...

Rotate String Problem

Problem Given two string s1 and s2 how will you check if s1 is a rotated version of s2 ? OR Assume you have a method isSubstring() which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring. OR Given two strings s and goal, return true if and only if s can become goal after some number of shifts on s. ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy