String

Strings in C

Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’.

char str[] = "GeeksforGeeks";
char str[50] = "GeeksforGeeks";
char str[] = {'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};
char str[14] = {'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};

Strings in C++

string subStr = s.substr(left, right);

// We can use a int array in c instead of hash map to store character count
int count[256] = {0};

// char to string
char c = 'a';
string s(1, c);

// string to int
stoi( str );

// extract words from string
auto pos = str.find(' ');
string word = str.substr(0, pos);
str = str.substr(pos+1);

while (i<j && isalnum(s[i]) == false) i++;
while (i<j && isalnum(s[j]) == false) j--;

if (tolower(s[i]) != tolower(s[j])) return false;

Technique 1: Sliding Window

A sliding window is an abstract concept commonly used in array/string problems. A window is a range of elements in the array/string which usually defined by the start and end indices.

A sliding window is a window slides its two boundaries to the certain direction.

  • Longest Substring Without Repeating Characters
  • Reverse words in a String III

Technique 2: Sliding Window Optimized

We can use hash map to help find the next position of the left pointer. But remember to check if the new position is smaller than the current left position.

  • Longest Substring Without Repeating Characters

Technique 3: Using Stack

By using stack we can reverse the words in the string. If we have to trim extra empty sapces between words, at the start or end, this technique can be applied.

  • Reverse Words in a String

Technique 4: Counting Sort

The idea of this technique is to use hash map to store the frequency of each character, use vector to store chars with same frequence.

  • Sort Characters by Frequency

results matching ""

    No results matching ""