Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Aarushi11H authored Dec 22, 2020
2 parents 543f175 + e01d82f commit fff837c
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 6 deletions.
29 changes: 29 additions & 0 deletions Algorithms/Sorting/Lily's Homework/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

def homework(n, arr):

pos = dict()
for i, num in enumerate(arr):
pos[num] = i

cnt = 0
sorted_arr = sorted(arr)

for i in range(len(arr)):
if arr[i] != sorted_arr[i]:
cnt += 1

new_idx = pos[sorted_arr[i]]
pos[arr[i]] = new_idx
arr[i], arr[new_idx] = arr[new_idx], arr[i]

return cnt

# input the number of elements of the array
n = int(input().strip())
# input elements of array
arr = list(map(int, input().strip().split()))

asc = homework(n, arr)
desc = homework(n, list(reversed(arr)))
# print the minimum number of swaps needed to make the array beautiful
print(min(asc, desc))
31 changes: 31 additions & 0 deletions Algorithms/Strings/Beautiful Binary String/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
We could use a greedy approach and starting from the
left everytime we see a 010 replace the last 0 with a 1
and continue
*/

import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {
// Read input from STDIN and Print output to STDOUT
Scanner input = new Scanner(System.in);
int n = input.nextInt(); // length of binary string
input.nextLine();
String s = input.nextLine(); // single binary string of length n
int switches = 0;

for(int i = 0; i < s.length()-2; i++) // left to right character of binary string
{
if(s.charAt(i) == '0' && s.charAt(i+1) == '1' && s.charAt(i+2) == '0')
{
switches++;
i += 2;
}
}
// minimum number of steps needed to make the string beautiful
System.out.println(switches);
}
}
35 changes: 35 additions & 0 deletions Algorithms/Strings/Highest Value Palindrome/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# input the number of digits and the maximum number of changes allowed
n,k=map(int,input().split(' '))
# input the n-digit string of numbers
num=input()
ss=list(num)
ch=[False for i in range(n)]
offset=0
cc=0
# print a string representation of the highest value achievable or -1
for i in range(n>>1):
if ord(ss[i])>ord(ss[n-1-i]):
ss[n-1-i]=ss[i]
ch[n-1-i]=True
cc+=1
elif ord(ss[i])<ord(ss[n-1-i]):
ss[i] = ss[n-1-i]
ch[i]=True
cc+=1
if cc>k:
print(-1)
else:
k-=cc
i=0
while k and i<(n>>1):
if ss[i]!='9':
if ch[i] or ch[n-i-1]:
ss[i]=ss[n-1-i]='9'
k-=1
elif k>=2:
ss[i] = ss[n - 1 - i] = '9'
k -= 2
i+=1
if n&1 and k:
ss[(n>>1)]='9'
print(''.join(ss))
49 changes: 49 additions & 0 deletions Algorithms/Strings/Making Anagrams/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

// makingAnagrams function
static int makingAnagrams(String s1, String s2) {
int[] freqs1 = new int[26];
int[] freqs2 = new int[26];
int deletion = 0;

for(int i = 0; i < s1.length(); i++)
freqs1[s1.charAt(i)-'a'] = freqs1[s1.charAt(i)-'a'] + 1; //Increment frequency of char at i
for(int i = 0; i < s2.length(); i++)
freqs2[s2.charAt(i)-'a'] = freqs2[s2.charAt(i)-'a'] + 1; //Increment frequency of char at i

for(int i = 0; i < 26; i++)
deletion += Math.abs(freqs1[i] - freqs2[i]); //Track the total deletions needed

// minimum number of characters which must be deleted to make the two strings anagrams of each other
return deletion;

}

private static final Scanner scanner = new Scanner(System.in);

// main function
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

String s1 = scanner.nextLine(); // first string

String s2 = scanner.nextLine(); // second string

int result = makingAnagrams(s1, s2); // function call

bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();

bufferedWriter.close();

scanner.close();
}
}
11 changes: 11 additions & 0 deletions Algorithms/Strings/String Construction/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/python3

import sys

# input number of strings
n = int(input().strip())
for a0 in range(n):
# input the strings
s = input().strip()
# print minimum cost of constructing new string
print(len(set(s)))
52 changes: 52 additions & 0 deletions Algorithms/Strings/The Love-Letter Mystery/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

// theLoveLetterMystery function
static int theLoveLetterMystery(String s) {
int count = 0;
int i = 0; // pointer at starting of string s
int j = s.length() - 1; // pointer at end of string s

// check for palindromic string
while (i <= j) {
if (s.charAt(i) != s.charAt(j)) {
count += Math.abs(((int) s.charAt(i)) - ((int) s.charAt(j)));
}
i++; j--;
}

// minimum number of operations corresponding to each test case
return count;

}

private static final Scanner scanner = new Scanner(System.in);

// main function
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

int q = scanner.nextInt(); // number of queries
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

for (int qItr = 0; qItr < q; qItr++) {
String s = scanner.nextLine(); // string

int result = theLoveLetterMystery(s);

bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
}

bufferedWriter.close();

scanner.close();
}
}
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ Please read [contribution guidelines](CONTRIBUTING.md) for contributing to the p
||[Tree: Square-Ten Tree](https://www.hackerrank.com/challenges/square-ten-tree/problem)|Hard|[View](Trees/Square-Ten%20Tree/solution.cpp)||[View](Trees/Square-Ten%20Tree/solution.py)|
||[Balanced Forest](https://www.hackerrank.com/challenges/balanced-forest/problem)|Hard|[View](Trees/Balanced%20Forest/solution.cpp)|[View](Trees/Balanced%20Forest/solution.java)|[View](Trees/Balanced%20Forest/solution.py)|
||[Kitty's Calculations on a Tree](https://www.hackerrank.com/challenges/kittys-calculations-on-a-tree/problem)|Advanced|[View](Trees/Kitty's%20Calculations%20on%20a%20Tree/solution.cpp)|[View](Trees/Kitty's%20Calculations%20on%20a%20Tree/solution.java)|[View](Trees/Kitty's%20Calculations%20on%20a%20Tree/solution.py)|
| Trie |
||[Contacts](https://www.hackerrank.com/challenges/contacts/problem)|Medium|[View](/Trie/Contacts/solution.cpp)|||
||[No Prefix Set](https://www.hackerrank.com/challenges/no-prefix-set/problem)|Hard|[View](/Trie/No%20Prefix%20Set/solution.cpp)|||
| Algorithms |
||[Mark and Toys]( https://www.hackerrank.com/challenges/mark-and-toys/problem)|Easy|[View](/Algorithms/Greedy/Mark%20and%20Toys/solution.cpp)|[View](/Algorithms/Greedy/Mark%20and%20Toys/solution.java)|[View](/Algorithms/Greedy/Mark%20and%20Toys/solution.py)|
||[Grading Students](https://www.hackerrank.com/challenges/grading/problem)|Easy|[View](/Algorithms/Greedy/Grading%20Students/solution.cpp)|[View](/Algorithms/Grading%20Students/solution.java)|[View](/Algorithms/Grading%20Students/solution.py)|
Expand Down Expand Up @@ -106,9 +109,9 @@ Please read [contribution guidelines](CONTRIBUTING.md) for contributing to the p
||[Sherlock and Array](https://www.hackerrank.com/challenges/sherlock-and-array/problem)|Easy|[View](Algorithms/Search/Sherlock%20and%20Array/solution.cpp)|[View](Algorithms/Search/Sherlock%20and%20Array/solution.java)|[View](/Algorithms/Search/Sherlock%20and%20Array/solution.py)|
||[Ice Cream Parlor](https://www.hackerrank.com/challenges/icecream-parlor/problem)|Easy|[View](Algorithms/Search/Ice%20Cream%20Parlor/solution.cpp)|[View](Algorithms/Search/Ice%20Cream%20Parlor/solution.java)|[View](/Algorithms/Search/Ice%20Cream%20Parlor/solution.py)|
||[Missing Numbers](https://www.hackerrank.com/challenges/missing-numbers/problem)|Easy|[View](Algorithms/Search/Missing%20Numbers/solution.cpp)|[View](Algorithms/Search/Missing%20Numbers/solution.java)|[View](/Algorithms/Search/Missing%20Numbers/solution.py)|
||[Making Anagrams](https://www.hackerrank.com/challenges/making-anagrams/problem)|Easy|[View](Algorithms/Strings/Making%20Anagrams/solution.cpp)|||
||[The Love-Letter Mystery](https://www.hackerrank.com/challenges/the-love-letter-mystery/problem)|Easy|[View](Algorithms/Strings/The%20Love-Letter%20Mystery/solution.cpp)|||
||[Beautiful Binary String](https://www.hackerrank.com/challenges/beautiful-binary-string/problem)|Easy|[View](Algorithms/Strings/Beautiful%20Binary%20String/solution.cpp)|||
||[Making Anagrams](https://www.hackerrank.com/challenges/making-anagrams/problem)|Easy|[View](Algorithms/Strings/Making%20Anagrams/solution.cpp)|[View](Algorithms/Strings/Making%20Anagrams/solution.java)||
||[The Love-Letter Mystery](https://www.hackerrank.com/challenges/the-love-letter-mystery/problem)|Easy|[View](Algorithms/Strings/The%20Love-Letter%20Mystery/solution.cpp)|[View](Algorithms/Strings/The%20Love-Letter%20Mystery/solution.java)||
||[Beautiful Binary String](https://www.hackerrank.com/challenges/beautiful-binary-string/problem)|Easy|[View](Algorithms/Strings/Beautiful%20Binary%20String/solution.cpp)|[View](Algorithms/Strings/Beautiful%20Binary%20String/solution.java)||
||[HackerRank in a String!](https://www.hackerrank.com/challenges/hackerrank-in-a-string/problem)|Easy|[View](Algorithms/Strings/HackerRank%20in%20a%20String!/solution.cpp)|[View](Algorithms/Strings/HackerRank%20in%20a%20String!/solution.java)|[View](Algorithms/Strings/HackerRank%20in%20a%20String!/solution.py)|
||[Funny String](https://www.hackerrank.com/challenges/funny-string/problem)|Easy|[View](Algorithms/Strings/Funny%20String/solution.cpp)|[View](Algorithms/Strings/Funny%20String/solution.java)|[View](Algorithms/Strings/Funny%20String/solution.py)|
||[Gemstones](https://www.hackerrank.com/challenges/gem-stones/problem)|Easy|[View](Algorithms/Strings/Gemstones/solution.cpp)|[View](Algorithms/Strings/Gemstones/solution.java)|[View](Algorithms/Strings/Gemstones/solution.py)|
Expand Down Expand Up @@ -152,9 +155,9 @@ Please read [contribution guidelines](CONTRIBUTING.md) for contributing to the p
||[Knapsack](https://www.hackerrank.com/challenges/unbounded-knapsack/problem)|Medium|[View](/Algorithms/Dynamic%20Programming/Knapsack/solution.cpp)|[View](/Algorithms/Dynamic%20Programming/Knapsack/solution.java)|[View](/Algorithms/Dynamic%20Programming/Knapsack/solution.py)|
||[The Bomberman Game](https://www.hackerrank.com/challenges/bomber-man/problem)|Medium|[View](/Algorithms/Implementation/The%20Bomberman%20Game/solution.cpp)|[View](/Algorithms/Implementation/The%20Bomberman%20Game/solution.java)|[View](/Algorithms/Implementation/The%20Bomberman%20Game/solution.py)|
||[Organizing Containers of Balls](https://www.hackerrank.com/challenges/organizing-containers-of-balls/problem)|Easy|[View](Algorithms/Organizing%20Containers%20of%20Balls/solution.cpp)|[View](Algorithms/Organizing%20Containers%20of%20Balls/solution.java)|[View](/Algorithms/Organizing%20Containers%20of%20Balls/solution.py)|
||[String Construction](https://www.hackerrank.com/challenges/string-construction/problem)|Easy||[View](/Algorithms/Strings/String%20Construction/solution.java)||
||[Highest Value Palindrome](https://www.hackerrank.com/challenges/richie-rich/problem)|Medium|||[View](/Algorithms/Strings/Highest%20Value%20Palindrome/solution.java)|
||[Lily's Homework](https://www.hackerrank.com/challenges/lilys-homework/problem)|Medium||[View](/Algorithms/Sorting/Lily's%20Homework/solution.java)||
||[String Construction](https://www.hackerrank.com/challenges/string-construction/problem)|Easy||[View](/Algorithms/Strings/String%20Construction/solution.java)|[View](/Algorithms/Strings/String%20Construction/solution.py)|
||[Highest Value Palindrome](https://www.hackerrank.com/challenges/richie-rich/problem)|Medium|||[View](/Algorithms/Strings/Highest%20Value%20Palindrome/solution.java)|[View](/Algorithms/Strings/Highest%20Value%20Palindrome/solution.py)|
||[Lily's Homework](https://www.hackerrank.com/challenges/lilys-homework/problem)|Medium||[View](/Algorithms/Sorting/Lily's%20Homework/solution.java)|[View](/Algorithms/Sorting/Lily's%20Homework/solution.py)|
| Contest Problems |
||[Squares of a Sorted Array](https://www.hackerrank.com/contests/coding-gym-mo0419/challenges/squares-of-a-sorted-array)|Medium|[View](/Contest%20Problems/Squares%20of%20a%20Sorted%20Array/solution.cpp)|[View](/Contest%20Problems/Squares%20of%20a%20Sorted%20Array/solution.java)|[View](/Contest%20Problems/Squares%20of%20a%20Sorted%20Array/solution.py)|
| Disjoint Sets |
Expand Down
37 changes: 37 additions & 0 deletions Trie/Contacts/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
#include <set>

using namespace std ;

int main ()
{
// input the number of options to perform
int n ;
cin >> n ;
set < string > s ;
set < string > :: iterator it ;
while ( n -- )
{
// input the operations
string a , b ;
cin >> a >> b ;
if ( a == "add" )
{
s.insert ( b ) ;
}
else
{
it = s.lower_bound ( b ) ;
int counter = 0 ;
int len = b.length () ;
while ( it != s.end () && b == (*it).substr ( 0 , len ) )
{
counter ++ ;
it ++ ;
}
// print the number of contact names starting with input string
cout << counter << endl ;
}
}
return 0 ;
}
73 changes: 73 additions & 0 deletions Trie/No Prefix Set/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

// Trie node
struct Node {
char data;
vector<Node*> nexts = vector<Node*> {};
Node(char c){
data = c;
}
};

// The trie class
class Trie {
public:
bool repeater = false;
Node* head = new Node('z');
void insert(const string& s){
bool working = false;
Node* tmp = head;
for(auto it=s.begin(); it < s.end(); ++it) {
vector<Node*>::iterator vit;
for(vit = tmp->nexts.begin(); vit < tmp->nexts.end(); ++vit) {
if((*vit)->data == *it){
tmp = *vit;
break;
}

}
if(tmp->nexts.size() == 0 && tmp != head && !working) {
repeater = true;
return;
}
if(vit == tmp->nexts.end()){
working = true;
auto vv = new Node(*it);
tmp->nexts.push_back(vv);
tmp = vv;
}
if(it==s.end()-1 && tmp->nexts.size() > 0){
repeater = true;
return;
}
}
}
};

// driver function to test the above functions
int main() {
// input number of strings in the set
int x;
cin >> x;

auto trie = Trie();
string s;
//print GOOD SET if the set is valid else, print BAD SET followed by the first string for which the condition fails
for(int i = 0; i < x ; ++i) {
// input the string
cin >> s;
trie.insert(s);
if(trie.repeater){
cout << "BAD SET" << "\n" << s << endl;
return 0;
}
}
cout << "GOOD SET" << endl;
return 0;
}

0 comments on commit fff837c

Please sign in to comment.