Thursday, August 18, 2022
HomeWordPress DevelopmentVerify if a personality is simply occurring as one single contiguous substring...

Verify if a personality is simply occurring as one single contiguous substring or not


View Dialogue

Enhance Article

Save Article

Like Article

Given string str of size N. The duty is to find out whether or not a personality is simply occurring as a single contiguous substring or not i.e., there shouldn’t exist another alphabet characters in between two identical alphabets within the string.

Examples:

Enter: “aAa”
Output: False
Rationalization: There exists ‘A’ in between ‘a’ and ‘a’. Therefore “aa” turns into discontinuous.

Enter: “aaAAbbBB”
Output: True
Rationalization: Completely different segments shaped from the given enter are “aa”, “AA”, “bb” and “BB” are all steady and don’t have any hurdles in between.

 

Strategy: To unravel the given drawback use the next concept:

Retailer the final incidence of every character in Hash-Map in order that it may be in contrast with its earlier incidence and discover the space between them. If this distance involves be greater than 1 then it’s discontinuous else steady.

Comply with the given steps to resolve the given drawback:

  • Traverse string str (say i).
  • If str[i] is current within the map
    • If the distinction between the final incidence and (i + 1) is equal to 1 then replace the final incidence of the i-th character.
    • Else return false.
  • Else replace the final incidence of i-th character within the Hash map as (i + 1).
  • Return True after the loop is over.

Beneath is the implementation for the above method:

C++14

#embody <bits/stdc++.h>

utilizing namespace std;

  

bool isContinuous(string& temp)

{

    

    

    unordered_map<char, int> last_Pos;

  

    for (int i = 0; i < temp.size(); i++) {

        

        

        if (last_Pos[temp[i]]) {

            

            

            

            if (i - last_Pos[temp[i]] + 1 <= 1) {

                last_Pos[temp[i]] = i + 1;

            }

            else {

                return 0;

            }

        }

        else {

            last_Pos[temp[i]] = i + 1;

        }

    }

    return 1;

}

  

int principal()

{

    string str = "aaAAbbBB";

    if (isContinuous(str)) {

        cout << "True";

    }

    else {

        cout << "False";

    }

    return 0;

}

Time Complexity: O(N)
Auxiliary House: O(N)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments