forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
326 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |