
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Meta Strings (Check if two strings can become same after a swap in one string) in C++
In this section, we will see how to check whether two strings are meta string or not. The meta strings are those strings that are very much similar. If we swap two elements in one string, then it will be matched with other string. Suppose two strings are “HELLO” and “OELLH”, then they are meta strings.
To check whether two strings are meta string or not, we have to follow these steps.
Steps −
If both strings are of different length, then return false
Else find a number of characters, that have not matched, also store the index of non-matched characters
If the count is greater than 2, then return false
Else swap these characters, then if two strings are the same, then return true, otherwise false.
Example
#include <iostream> using namespace std; bool areTheyMetaString(string s1, string s2) { int s1_len = s1.length(); int s2_len = s2.length(); if (s1_len != s2_len) return false; int prev = -1, curr = -1; int count = 0; for (int i=0; i<s1_len; i++) { if (s1[i] != s2[i]) { count++; // number of unmatched characters if (count > 2) return false; prev = curr; curr = i; } } return (count == 2 && s1[prev] == s2[curr] && s1[curr] == s2[prev]); } int main() { string s1 = "HELLO", s2 = "OELLH"; if(areTheyMetaString(s1, s2)){ cout << "Meta Strings"; } else { cout << "Not Meta Strings"; } }
Output
Meta Strings
- Related Articles
- Count of strings that become equal to one of the two strings after one removal in C++
- Program to check whether one string swap can make strings equal or not using Python
- C program to swap two strings
- C Program to check if two strings are same or not
- Swap two Strings without using temp variable in C#
- Check whether given string can be generated after concatenating given strings in Python
- Check if edit distance between two strings is one in Python
- Check if a given string can be formed by two other strings or their permutations
- How can I swap two strings without using a third variable in Java?
- C function to Swap strings
- Check if given string can be split into four distinct strings in Python
- Python - Check if two strings are isomorphic in nature
- Check if frequency of all characters can become same by one removal in Python
- C++ program to check we can make two strings equal by swapping from third string
- How to check if two strings are equal in Java?

Advertisements