Skip to content

Instantly share code, notes, and snippets.

@tylersloeper
Last active December 28, 2016 08:21
Show Gist options
  • Save tylersloeper/40dca1140419b6a9c6da64ef650995be to your computer and use it in GitHub Desktop.
Save tylersloeper/40dca1140419b6a9c6da64ef650995be to your computer and use it in GitHub Desktop.
Hacker Rank: Strings: Making Anagrams, (in c)
/**
The challenge:
https://www.hackerrank.com/challenges/ctci-making-anagrams
**/
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <ctype.h>
int main()
{
int match = 0;
int i, k;
int lengthofa = 0;
int lengthofb = 0;
int deletions;
char* a = (char *)malloc(512000 * sizeof(char));
scanf("%s",a);
char* b = (char *)malloc(512000 * sizeof(char));
scanf("%s",b);
//check for length of char*
for(i=0;i<10000;i++)
{
if(isalpha(a[i]))
{
lengthofa++;
}
}
for(k=0;k<10000;k++)
{
if(isalpha(b[k]))
{
lengthofb++;
}
}
/*
*/
for(i=0;i<lengthofa;i++)
{
for(k=0;k<lengthofb;k++)
{
if(a[i] == b[k])
{
match++;
//after counting the letter, delete it so it is not recounted. The list is not conserved.
b[k] = 0;
//after finding a match, exit k for loop and start again with the next letter in a[i].
k = lengthofb -1;
}
else
{
}
}
}
deletions = (lengthofa - match) + (lengthofb - match);
//printf("deletions ");
printf("%d \n", deletions);
/*
//error checking
printf("matches :%d \n", match);
printf("lengthofa: %d \n", lengthofa);
printf("lengthofb: %d \n", lengthofb);
printf("%c \n", a[0]);
printf("%c \n", a[1]);
printf("%c \n", a[2]);
*/
/*
//incorrect attempt to size array
lengthofa = sizeof(a)/sizeof(char);
lengthofb = (sizeof(b))/(sizeof(char));
*/
return 0;
}
/**
The challenge:
https://www.hackerrank.com/challenges/ctci-making-anagrams
**/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
static void Main(String[] args)
{
string a = Console.ReadLine();
string b = Console.ReadLine();
int lena = a.Length;
int lenb = b.Length;
int i;
int j;
int deletions =0;
//int countmatches =0;
int[] arraya = new int[lena];
int[] arrayb = new int[lenb];
//go through whole list and 0 out matches between both lists n^2. then go through each list and count the number of non zeroed out character 2n. This number is the number of deletions.
//populate array that can be editted.
for(i=0; i<lena;i++)
{
arraya[i] = a[i];
//Console.WriteLine("{0}", (char)arraya[i]); //debug.log
}
for(j=0;j<lenb;j++)
{
arrayb[j] = b[j];
//Console.WriteLine("{0}", (char)arrayb[j]); //debug.log
}
for(i=0; i<lena;i++)
{
//Console.WriteLine(a[i]); //debug.log
for(j=0;j<lenb;j++)
{
if(arraya[i] == arrayb[j])
{
//removes matches
arraya[i] = 0;
arrayb[j] = 0;
//exit instance of loop
break;
}
}
}
//count all deletions
for(i=0; i<lena;i++)
{
if(arraya[i] != 0)
{
deletions = deletions +1;
//Console.WriteLine(deletions); //debug
}
}
for(j=0;j<lenb;j++)
{
if(arrayb[j] != 0)
{
deletions = deletions +1;
//Console.WriteLine(deletions); //debug
}
}
Console.WriteLine(deletions);
/* //debug.log
for(i=0; i<lena;i++)
{
Console.WriteLine("{0}", (char)arraya[i]); //debug.log
}
for(j=0;j<lenb;j++)
{
Console.WriteLine("{0}", (char)arrayb[j]); //debug.log
}
*/
}
}
@tylersloeper
Copy link
Author

screenshot 2016-12-28 03 21 09

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment