Você está na página 1de 4

Given a string S1, convert it to another string S2 (Anagram) by swapping only ad

jacent elements.
Print all the intermediate strings formed.
eg: s1: abcde
s2: bcdae
output: bacde,bcade, bcdae.
================================================================================
=======
#include <iostream>
void convertString(std::string& str1, std::string& str2) {
if (str1.empty() || str2.empty()) {
std::cout << "Empty String Input" << std::endl;
return;
}
if (str1.size() != str2.size()) {
std::cout << "Unequal String Size in Input" << std::endl;
return;
}
if (str1.compare(str2) == 0) {
std::cout << "Equal String in Input" << std::endl;
return;
}
std::string workStr1(str1);
for (int i=0; i<str2.size(); i++) {
int j=i;
while ((j < workStr1.size()) && (workStr1[j] != str2[i]))
++j;
std::cout << "j = " << j << " i = " << i << std::endl;
if (i ==j)
continue;
for (int k=j; k>i; k--) {
char c = workStr1.at(k);
workStr1.at(k) = workStr1.at(k-1);
workStr1.at(k-1) = c;
std::cout << workStr1 << std::endl;
}
}
}
int main() {
std::string str1("abcdea");
std::string str2("deaabc");
convertString(str1, str2);

}
==========================================================================
1. Create a temp string and store concatenation of str1 to
str1 in temp.
temp = str1.str1
2. If str2 is a substring of temp then str1 and str2 are
rotations of each other.
Example:
str1 = "ABACD"
str2 = "CDABA"
temp = str1.str1 = "ABACDABACD"
Since str2 is a substring of temp, str1 and str2 are
rotations of each other.

Array
Bit Magic
C/C++
Articles
GFacts
Linked List
MCQ
Misc
Output
String
Tree
Graph
A Program to check if strings are rotations of each other or not
Given a string s1 and a string s2, write a snippet to say whether s2 is a rotati
on of s1 using only one call to strstr routine?
(eg given s1 = ABCD and s2 = CDAB, return true, given s1 = ABCD, and s2 = ACBD ,
return false)
Algorithm: areRotations(str1, str2)
1. Create a temp string and store concatenation of str1 to
str1 in temp.
temp = str1.str1
2. If str2 is a substring of temp then str1 and str2 are
rotations of each other.
Example:
str1 = "ABACD"
str2 = "CDABA"
temp = str1.str1 = "ABACDABACD"
Since str2 is a substring of temp, str1 and str2 are
rotations of each other.
Implementation:

# include <stdio.h>
# include <string.h>
# include <stdlib.h>
/* Function checks if passed strings (str1 and str2)
are rotations of each other */
int areRotations(char *str1, char *str2)
{
int size1 = strlen(str1);
int size2 = strlen(str2);
char *temp;
void *ptr;
/* Check if sizes of two strings are same */
if (size1 != size2)
return 0;
/* Create a temp string with value str1.str1 */
temp = (char *)malloc(sizeof(char)*(size1*2 + 1));
temp[0] = '\0';
strcat(temp, str1);
strcat(temp, str1);
/* Now check if str2 is a substring of temp */
ptr = strstr(temp, str2);
free(temp); // Free dynamically allocated memory
/* strstr returns NULL if the second string is NOT a
substring of first string */
if (ptr != NULL)
return 1;
else
return 0;
}
/* Driver program to test areRotations */
int main()
{
char *str1 = "AACD";
char *str2 = "ACDA";
if (areRotations(str1, str2))
printf("Strings are rotations of each other");
else
printf("Strings are not rotations of each other");
getchar();
return 0;
}
==========================================================================
private void transpose(String source, String destination) {
if(source.length() != destination.length()) {
return;
}
for(int i=0;i< source.length()-1;i++) {
char a= source.charAt(i);

char b = source.charAt(i+1);
if(checkCharOrder(a , b, destination)) {
source = swapAdjacent(i,i+1,source);
}
if(source.equals(destination)) {
System.out.println("Strings match now " + source
+ " "+ destination );
return;
}
}
}
private String swapAdjacent(int i, int j, String source) {
StringBuilder string = new StringBuilder(source);
char temp = source.charAt(i);
string.setCharAt(i, source.charAt(i+1));
string.setCharAt(i+1, temp);
System.out.println(string.toString());
return string.toString();
}
private boolean checkCharOrder(char a, char b, String destination) {
int indexOfA = destination.indexOf(a);
int indexOfB = destination.indexOf(b);
return ( indexOfA > indexOfB);
}

Você também pode gostar