Convert String to ZigZag Bottom Up

Problem The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) Example P A H N A P L S I I G Y I R ```And then read line by line: `"PAHNAPLSIIGYIR"` Write the code that will take a string and make this conversion given a number of rows: string convert(string text, int nRows); ````convert(“PAYPALISHIRING”, 3) should return "PAHNAPLSIIGYIR"`. [Read More]

Given two strings str1 and str2, write code to find out if all non-unique elements in str2 are in str1?

Problem Given two strings str1 and str2, write code to find out if all non-unique elements in str2 are in str1? Solution Method 1 - Using hashset on str2 and boolean array or hashset on str2 Break down the problem into two tasks. i) Finding the non-unique i.e. repeating elements in str 1. Hashing can be used for this purpose. ii) Finding if all the characters hashed in the first string are available in the second string. [Read More]

Given an array filled with char elements, find the max length of continuous white space

Problem Given array filled with char elements, can you suggest a most efficient way to find the max length of continuous white space? Solution Scan the array left to right, keep a count of white space. When you reach a non-whitespace character, check that count against the current max; if it’s higher, it becomes the new max. Set the count back to zero, continue scanning. Time complexity - O(n) [Read More]

A Needle in the Haystack

Problem  The program has to detect all occurences of the needle in the haystack. It should take the needle and the haystack as input, and output the positions of each occurence, as shown below. Input The input consists of a number of test cases. Each test case is composed of three lines, containing: the length of the needle, the needle itself, the haystack. Output For each test case your program should output all positions of the needle’s occurences within the haystack. [Read More]

Get Sentence from raw text

DP Code Given Above Has Some Issue I Fixed It: st… PaNThErA - Dec 0, 2015DP Code Given Above Has Some Issue I Fixed It: static boolean wordBreak(String str, Set tokenMap) { int size = str.length(); if (size == 0) return true; // Create the DP table to store results of subroblems. The value wb[i] // will be true if str[0..i-1] can be segmented into dictionary words, // otherwise false. [Read More]

Get Sentence from raw text

Problem Given a raw sentence without spaces and dictionary of words, break the raw sentence to sentence of words. String getSentence(String text, Setdictionary); // text is a string without spaces, you need to insert spaces into text, so each word seperated by the space in the resulting string exists in the dictionary, return the resulting string This problem is also known as wordbreaker problem. Example // getSentence(“iamastudentfromwaterloo”, {“from, “waterloo”, “hi”, “am”, “yes”, “i”, “a”, “student”}) -> “i am a student from waterloo” [Read More]

Determine if a string has all unique characters

Problem: Implement an algorithm to determine if a string has all unique characters. Solution Solution1 - My first algorithm is a straightforward, brute force approach. Basically, 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. bool uniqueChars(string myString) { for (int x = 0; x < myString. [Read More]

Print letters followed by their frequency (Run Length Encoding)

Given a string ,Write a program to print letter followed by it’s frequency.This is also knows as Run Length Encoding. Example: Input aaabcc Output a3b1c2 Algorithm: 1.Pick the first character from the string and print it. 2.Count the number of subsequent occurrences of the picked character and then print the count. 3.Pick the next character and repeat step 2 till we reach end of the String. #include <iostream> #include<string> using namespace std; int main() { string str = "aaabcc"; int count=0; int i=0,j; int l = str. [Read More]

Remove duplicate characters in a string

Problem Given a string, Write a program to remove duplcate characters from the string. Input String : kodeknight Output String : kodenight Solution Method 1 : Brute force 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 (strii == strjj) break; if (j == tail) { strtailtail = strii; ++tail; } } strtailtail = '\\0'; } Time Complexity : O(N^2) [Read More]

Find first non repeating character in a string

You are given a string, find its first non-repeating character?

Algorithm:

  1. Scan the string from left to right and construct the count array.
  2. Again, scan the string from left to right and check for count of each character, if you find an element who’s count is 1, return it.

Code :

#include<stdlib.h>  
#include<stdio.h>  
#define NO\_OF\_CHARS 256  
  
/\* The function returns index of first non-repeating  
   character in a string \*/  
int getFirstNonRepeating(char \*str)  
{  
 int \*count = (int \*)calloc(sizeof(int), NO\_OF\_CHARS);  
 int i;  
 for (i = 0; \*(str+i);  i++)  
   count\[\*(str+i)\]++;  
 int index = -1;  
  
 for (i = 0; \*(str+i);  i++)  
 {  
  if(count\[\*(str+i)\] == 1)  
  {  
    index = i;  
    break;  
  }  
 }  
 return index;  
}  

Time complexity: O(N)