Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5d59a93

Browse files
authored
Add files via upload
1 parent 3a40f82 commit 5d59a93

23 files changed

+736
-0
lines changed

Week6/A-Strings/AllSubStrings.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
public class Solution {
3+
4+
public static void printSubstrings(String str) {
5+
//Your code goes here
6+
for(int i=0;i<str.length();i++)
7+
{
8+
String res="";
9+
for(int j=i;j<str.length();j++)
10+
{
11+
res = res + str.charAt(j);
12+
System.out.println(res);
13+
}
14+
15+
res="";
16+
}
17+
}
18+
19+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.util.HashMap;
2+
public class Solution {
3+
4+
public static boolean isPermutation(String str1, String str2) {
5+
//Your code goes here
6+
HashMap <Character,Integer> hm=new HashMap<>();
7+
int l1=str1.length();
8+
int l2=str2.length();
9+
for(int i=0;i<l1;i++)
10+
{
11+
char c=str1.charAt(i);
12+
if(!(hm.containsKey(c)))
13+
{
14+
hm.put(c,1);
15+
16+
}
17+
else{
18+
int v=hm.get(c);
19+
hm.put(c,v+1);
20+
}
21+
}
22+
for(int i=0;i<l2;i++)
23+
{
24+
char c=str2.charAt(i);
25+
if(!(hm.containsKey(c)))
26+
{
27+
return false;
28+
}
29+
int v=hm.get(c);
30+
v-=1;
31+
if(v!=0){
32+
hm.put(c,v);
33+
}
34+
else{
35+
hm.remove(c);
36+
}
37+
38+
}
39+
return hm.isEmpty();
40+
}
41+
42+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
public class Solution {
2+
public static String getCompressedString(String str) {
3+
// Write your code here.
4+
int l=str.length();
5+
int count=0;
6+
String res="";
7+
int j=0;
8+
for(int i=0;i<l;i++)
9+
{
10+
char c=str.charAt(i);
11+
res+=c;
12+
count=0;
13+
for( j=i;j<l;j++)
14+
{
15+
if(c==str.charAt(j))
16+
{
17+
count+=1;
18+
}
19+
else{
20+
break;
21+
}
22+
}
23+
if(count!=1)
24+
{
25+
res+=count;
26+
}
27+
i=j-1;
28+
}
29+
return res;
30+
}
31+
32+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.HashMap;
2+
public class Solution {
3+
4+
public static char highestOccuringChar(String str) {
5+
//Your code goes here
6+
int l=str.length(),max=-1;
7+
char ch=' ';
8+
HashMap<Character,Integer> hash=new HashMap<>();
9+
for(int i=0;i<l;i++)
10+
{
11+
char c=str.charAt(i);
12+
int cou=1;
13+
if(!(hash.containsKey(c)))
14+
{
15+
hash.put(c,1);
16+
}
17+
else{
18+
cou=hash.get(c);
19+
hash.put(c,cou+1);
20+
}
21+
22+
23+
}
24+
for(int j=0;j<l;j++)
25+
{
26+
char c=str.charAt(j);
27+
int count=hash.get(c);
28+
if(max<count)
29+
{
30+
max=count;
31+
ch=c;
32+
}
33+
}
34+
return ch;
35+
}
36+
37+
}w
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
public class Solution {
3+
4+
public static String removeAllOccurrencesOfChar(String str, char ch) {
5+
// Your code goes here
6+
String temp="";
7+
for(int i=0;i<str.length();i++)
8+
{
9+
char c=str.charAt(i);
10+
if(c!=ch)
11+
{
12+
temp+=c;
13+
}
14+
}
15+
return temp;
16+
}
17+
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
public class Solution {
3+
4+
public static String removeConsecutiveDuplicates(String str)
5+
{
6+
//Your code goes here
7+
String st=""+str.charAt(0);
8+
for(int i=1;i<str.length();i++)
9+
{
10+
char c=str.charAt(i);
11+
12+
char c2=st.charAt(st.length()-1);
13+
if(c!=c2)
14+
{
15+
st = st + c;
16+
}
17+
}
18+
return st;
19+
}
20+
21+
}

Week6/A-Strings/CountWords.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
public class Solution {
3+
4+
public static int countWords(String str)
5+
{
6+
//Your code goes here
7+
String s[]=str.split(" ");
8+
int len=s.length;
9+
return len;
10+
}
11+
12+
}

Week6/A-Strings/ReverseEachWord.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
public class Solution {
3+
4+
public static String reverseEachWord(String str)
5+
{
6+
//Your code goes here
7+
str=str.trim();
8+
String s="";
9+
String res="";
10+
for(int i=0;i<str.length();i++)
11+
{
12+
if(str.charAt(i)==' ')
13+
{
14+
res=res+s+" ";
15+
s="";
16+
}
17+
else{
18+
s=str.charAt(i)+s;
19+
}
20+
21+
}
22+
res=res+s;
23+
return res.trim();
24+
}
25+
26+
}

Week6/A-Strings/StringPalindrome.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
public class Solution {
3+
4+
public static boolean isPalindrome(String str) {
5+
//Your code goes here
6+
int i=0,j=str.length()-1;
7+
while(i<j)
8+
{
9+
if(str.charAt(i)!=(str.charAt(j)))
10+
{
11+
return false;
12+
}
13+
else{
14+
i+=1;
15+
j-=1;
16+
}
17+
}
18+
return true;
19+
}
20+
21+
}

Week6/A-Strings/StringReverse.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.* ;
2+
import java.io.*;
3+
4+
class Solution {
5+
6+
public static String stringReverse(char[] arr)
7+
{
8+
// Write your code here.
9+
String s="";
10+
for(int i=0;i<arr.length;i++)
11+
{
12+
s = arr[i] + s;
13+
}
14+
return s;
15+
16+
}
17+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
public class Solution {
3+
4+
public static void wavePrint(int mat[][]){
5+
//Your code goes here
6+
int n=mat.length;
7+
if(n==0)
8+
{
9+
return;
10+
}
11+
int m=mat[0].length;
12+
int sum=-1;
13+
for(int i=0;i<m;i++)
14+
{
15+
sum=0;
16+
if(i%2==1)
17+
{
18+
for(int j=n-1;j>=0;j--)
19+
{
20+
System.out.print(mat[j][i]+" ");
21+
}
22+
}
23+
else{
24+
for(int j=0;j<n;j++)
25+
{
26+
System.out.print(mat[j][i]+" ");
27+
}
28+
}
29+
30+
}
31+
return;
32+
}
33+
34+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.ArrayList;
2+
public class Solution {
3+
4+
public static void rowWiseSum(int[][] mat)
5+
{
6+
//Your code goes here
7+
int n=mat.length;
8+
if(n==0)
9+
{
10+
return;
11+
}
12+
int m=mat[0].length;
13+
int arr[]=new int[n];
14+
int sum=-1,k=-1;
15+
for(int i=0;i<n;i++)
16+
{
17+
sum=0;
18+
for(int j=0;j<m;j++)
19+
{
20+
sum+=mat[i][j];
21+
}
22+
arr[k+=1]=sum;
23+
}
24+
for(int i=0;i<n;i++)
25+
{
26+
System.out.print(arr[i]+" ");
27+
}
28+
System.out.println();
29+
return;
30+
}
31+
32+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
public class Solution {
3+
4+
public static void totalSum(int[][] mat) {
5+
//Your code goes here
6+
int n=mat.length;
7+
if(n==0)
8+
{
9+
System.out.println("0");
10+
return;
11+
}
12+
int sum=0;
13+
for(int i=0;i<n;i++)
14+
{
15+
for(int j=0;j<n;j++)
16+
{
17+
if(i==0 || j==0 || i==n-1 || j==n-1)
18+
{
19+
sum+=mat[i][j];
20+
}
21+
else if(i==j || (i+j+1)==n )
22+
{
23+
sum+=mat[i][j];
24+
}
25+
else{
26+
sum+=0;
27+
}
28+
}
29+
}
30+
System.out.println(sum);
31+
return;
32+
}
33+
34+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.* ;
2+
import java.io.*;
3+
4+
public class Solution {
5+
6+
public static int[][] transpose(int m, int n, int[][] mat) {
7+
// Write your code here
8+
int temp[][]=new int[n][m];
9+
for(int i=0;i<n;i++)
10+
{
11+
for(int j=0;j<m;j++)
12+
{
13+
temp[i][j]=mat[j][i];
14+
}
15+
}
16+
return temp;
17+
}
18+
19+
}

0 commit comments

Comments
 (0)