leonzakaim
BAN USER
Comments (2)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
int Func2()
{
char source[] = "abc";
char search[] = "cba";
std::vector<std::vector<std::string>> stringsCount;
int lenght = strlen(source);
int i = 0;
char *ss = new char[lenght + 1]{'\0'};
for (int j = 1; j < lenght; j++)
{
strcpy_s(ss,sizeof(char)*lenght+1, source);
std::vector < std::string> strings;
for (i = 0; i<lenght; i += j)
{
char temp = ss[0];
ss[0] = ss[i];
ss[i] = temp;
strings.push_back(std::string(ss));
if (!strcmp(ss, search))
{
stringsCount.push_back(strings);
break;
}
}
}
if (stringsCount.size() > 0)
{
int min = stringsCount[0].size();
for (int i=0; i < stringsCount.size(); i++)
{
if (min>stringsCount[i].size())
min = stringsCount[i].size();
}
return min;
}
return -1;
}
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
#include "stdafx.h"// A C program to print all anagarms together
- leonzakaim February 19, 2018#include <stdio.h>
#include <vector>
using namespace std;
vector<vector<char>> worldGlobal{
vector<char>{0,1,0,1,0,1,0},
vector<char>{0,1,0,0,0,1,0},
vector<char>{0,1,1,0,1,0,1}
};
char **arrVisited = nullptr;
int height;
int width;
int counter = 0;
void VisitAll(int h, int w, const vector<vector<char>> &world)
{
if (h >= height)
return;
if (w >= width)
return;
if (w < 0)
return;
if (h < 0)
return;
if (worldGlobal[h][w] == 0)
return;
if (arrVisited[h][w] == 1)
return;
else if (worldGlobal[h][w] == 1 && arrVisited[h][w]==0)
{
arrVisited[h][w] = 1;
}
VisitAll(h+1, w, worldGlobal);
VisitAll(h, w+1, worldGlobal);
}
void Getlands(int h,int w, const vector<vector<char>> &world)
{
if (worldGlobal[h][w] == 1 && arrVisited[h][w] == 0)
{
arrVisited[h][w] = 2; // find the root
counter++;
VisitAll(h, w, worldGlobal);
}
}
void main()
{
height = worldGlobal.size();
width = worldGlobal[0].size();
for (int i = 0; i < height; i++)
{
for (int j = 0; j <width; j++)
{
int y = worldGlobal[i][j];
printf("%d ", y);
}
printf("\n");
}
printf("\n");
if (arrVisited == nullptr)
{
arrVisited = new char*[height];
for (int i = 0; i < height; i++)
arrVisited[i] = new char[width]{ 0 };
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
Getlands(i, j, worldGlobal);
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j <width; j++)
{
int y = arrVisited[i][j];
printf("%d ",y);
}
delete [] arrVisited[i];
arrVisited[i] = nullptr;
printf("\r\n");
}
printf("Number islands %d \r\n", counter);
delete [] arrVisited;
}