Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions BackTracking/sudoku.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Problem from GFG

bool isSafe(int i, int j,int num,int grid[9][9]){
for(int p=0;p<9;p++){
if(grid[i][p]==num){
return false;
}
if(grid[p][j]==num){
return false;
}
}

int s = sqrt(9);
i = i - (i%s);
j = j - (j%s);
for(int l =0;l<s;l++){
for(int m=0;m<s;m++){
if(grid[l+i][m+j]==num){
return false;
}
}
}
return true;
}
bool SolveSudoku(int grid[N][N])
{
bool flag = false;
int i,j;
for( i=0;i<N;i++){
for( j=0;j<N;j++){
if(grid[i][j]==0){
flag = true;
break;
}
}
if(flag){
break;
}
}
if(i==N && j==N){
return true;
}
for(int k=1;k<=N;k++){
if(isSafe(i,j,k,grid)){
grid[i][j] = k;
if(SolveSudoku(grid)){
return true;
}

grid[i][j] = 0;
}
}
return false;
}

void printGrid (int grid[N][N])
{
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
cout<<grid[i][j]<<" ";
}
}

}
18 changes: 18 additions & 0 deletions Dynamic_Programming/sum_of_substrings_of_a_given_num.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Algorithm to find the sum of all substrings of a given number
// Example: Input: S = 1234 Output: 1670 Explanation: Sum = 1 + 2 + 3 + 4 + 12 + …

long long sumSubstrings(string s){
const int p = 1000000007;
int n = s.length();
long long dp[n];
dp[0] = (s[0]-'0');
long long sum=dp[0];
for(int i=1;i<n;i++){
dp[i] = ((i+1)*(s[i]-'0') + 10*dp[i-1])%p;
sum+=dp[i];
if(sum>p){
sum = (sum%p);
}
}
return sum;
}
37 changes: 37 additions & 0 deletions Hashing/Group_anagrams_together.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Practice que from gfg
// Given an array of words, print the count of all anagrams together in sorted order (increasing order of counts).
// For example, if the given array is {“cat”, “dog”, “tac”, “god”, “act”}, then grouped anagrams are “(dog, god) (cat, tac, act)”. So the output will be 2 3.



#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while(t--){
vector<int> v;
int N;
cin>>N;
string a[N];
for(int i=0;i<N;i++){
cin>>a[i];
}
unordered_map<string,int> um;
for(int i=0;i<N;i++){
sort(a[i].begin(),a[i].end());
um[a[i]]++;
}
for(auto x:um){
v.push_back(x.second);
}
sort(v.begin(),v.end());
for(int i=0;i<v.size();i++){
cout<<v[i]<<" ";
}
cout<<endl;
}
return 0;
}