Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create AnyBaseToAnyBase.java #30

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
32 changes: 32 additions & 0 deletions Anagram.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.*;

public class Anagram {

public static boolean isAnagram(String s1, String s2){
if(s1.length() != s2.length()) //If length of both strings are not same then they can't be anagram
return false;

int []fre_for_s1 = new int[256];//Frequancy array for string 1
int []fre_for_s2 = new int[256];//Frequancy array for string 2

for(int i = 0; i < s1.length(); i++){ //To count frequancy of each character in both strings
fre_for_s1[s1.charAt(i)]++;
fre_for_s2[s2.charAt(i)]++;
}
for (int i = 0; i < 256; i++) { //if there exist any charactor whose frequancy is different in both strings then it can't be anagram
if(fre_for_s1[i] != fre_for_s2[i])
return false;
}
//if we can't found such character with different frequancy then we return true.
return true;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.next(); //Accepting String 1
String s2 = sc.next(); //Accepting String 2

System.out.println(isAnagram(s1,s2));
sc.close();
}
}
25 changes: 25 additions & 0 deletions AnyBaseToAnyBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// This is a java program to convert any base number into any base
import java.util.*;
public class AnyBaseToAnyBase {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);

int n = sc.nextInt();//Scan the number
int b1 = sc.nextInt();//Base of current number
int b2 = sc.nextInt();//base in which you want to convert
int ans = Any(n,b1,b2);//answer
System.out.println(ans);
sc.close();
}
public static int Any(int n, int b1, int b2) {
int answer=0,last_degit,pos=1;

while (n>0) {
last_degit = n%b2;
n/=b2;
answer += last_degit*pos;
pos *= b1;
}
return answer;
}
}