Check if a Character is Only Occurring as One Single Contiguous Substring or Not


In this problem, we will check whether all characters are present continuously in the given string. We will use the map data structure to solve the problem.

The map will keep track of the last index of the particular character, and based on the last index of the current character, we will decide whether the string contains contiguous characters.

Problem statement – We have given a string alpha of length N containing the lowercase and uppercase alphabetical characters. We need to check whether the given string is contiguous. The string is contiguous only if it contains all characters as a contiguous substring. So, there should not be any other character between the same character.

Sample examples

Input

alpha = "PPQQpp"

Output

 Yes

Explanation – The string contains all characters continuously. So, it prints ‘Yes’.

Input

alpha = "PQPQQpp"

Output

‘No’

Explanation – The ‘Q’ is between two ‘P’. So, the string is not contiguous.

Input

alpha = "PpPQQpp"

Output

‘No’

Explanation – The lowercase ‘p’ is in between the uppercase ‘P’. So, the string is not contiguous.

Approach 1

In this approach, we will use the map to store the position of the string character, representing the last position of the character in the string. If the last position stored in the map is not adjacent, we can say that string is not contiguous.

Algorithm

Step 1 – Define the ‘position’ map to store the character as a key and the integer as a value.

Step 2 – Start iterating the given string.

Step 3 – If a character exists as a map key, follow the steps below. Otherwise, add a character to the map with an index + 1 value.

Step 4 – Take the last position of the current character from the map, and if the (index – last position + 1) is less than or equal to 1, update the position of the character using the current index.

Step 5 – Return false, as the string is not contiguous.

Step 6 – At last, return true if the string is valid.

Example

#include <bits/stdc++.h>
using namespace std;

bool checkContinuous(string alpha) {
	unordered_map<char, int> position;
	for (int p = 0; p < alpha.length(); p++) {
		// Check whether the character present in the hashmap or not
		if (position[alpha[p]]) {
			// Check whether the last occurrence is adjacent or not
			if (p - position[alpha[p]] + 1 <= 1) {
				position[alpha[p]] = p + 1;
			} else {
				return false;
			}
		}
		else {
			position[alpha[p]] = p + 1; // Add the position of the character
		}
	}
	return true;
}
int main() {
	string alpha = "PPQQpp";
	if (checkContinuous(alpha)) {
		cout << "Yes, String is continuous!";
	} else {
		cout << "No, String is not continuous!";
	}
	return 0;
}

Output

Yes, String is continuous!

Time complexity – O(N) for traversing the string.

Space complexity – O(N) for storing the last character position.

We used the map data structure to solve the problem, but programmers may use the array data structure to store the frequency of characters. Furthermore, we can solve the problem using two nested loops, but its time complexity can be more than the approach explained in this tutorial.

Updated on: 17-Jul-2023

97 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements