Skip to content

Commit ef98b27

Browse files
Accepted
1 parent d53070c commit ef98b27

File tree

5 files changed

+189
-0
lines changed

5 files changed

+189
-0
lines changed

problems/src/PascalsTriangle.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
5+
/**
6+
* Created by gouthamvidyapradhan on 25/03/2017.
7+
* Accepted
8+
*/
9+
public class PascalsTriangle
10+
{
11+
public static void main(String[] args) throws Exception
12+
{
13+
System.out.println(new PascalsTriangle().getRow(3));
14+
}
15+
16+
public List<Integer> getRow(int rowIndex)
17+
{
18+
int k = rowIndex;
19+
if(k == 0)
20+
return Arrays.asList(1);
21+
else if(k == 1)
22+
return Arrays.asList(1, 1);
23+
else if (k == 2)
24+
return Arrays.asList(1, 2, 1);
25+
List<Integer> result = new ArrayList<>();
26+
result.add(2);
27+
k = k - 2;
28+
int p, c;
29+
while(k-- > 0)
30+
{
31+
p = 1;
32+
int i = 0;
33+
for(int l = result.size(); i < l; i ++)
34+
{
35+
c = result.get(i);
36+
result.set(i, p + c);
37+
p = c;
38+
}
39+
result.add(p + 1);
40+
}
41+
result.add(0, 1);
42+
result.add(1);
43+
return result;
44+
}
45+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Created by gouthamvidyapradhan on 25/03/2017.
3+
* Accepted
4+
*/
5+
public class RepeatedSubstringPattern
6+
{
7+
/**
8+
* Main method
9+
* @param args
10+
* @throws Exception
11+
*/
12+
public static void main(String[] args) throws Exception
13+
{
14+
System.out.println(new RepeatedSubstringPattern().repeatedSubstringPattern("a"));
15+
}
16+
17+
public boolean repeatedSubstringPattern(String s)
18+
{
19+
boolean found;
20+
for(int i = 1, l = s.length(); i < l; i ++)
21+
{
22+
found = true;
23+
String subI = s.substring(0, i);
24+
int j = i;
25+
if(j >= l) return false;
26+
while(j < l)
27+
{
28+
if((j + i) >= l + 1)
29+
return false;
30+
String subJ = s.substring(j , j + i);
31+
if(!subI.equals(subJ))
32+
{
33+
found = false;
34+
break;
35+
}
36+
j += i;
37+
}
38+
if(found) return true;
39+
}
40+
return false;
41+
}
42+
}

problems/src/SortCharByFrequency.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.*;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 25/03/2017.
5+
* Accepted
6+
*/
7+
public class SortCharByFrequency
8+
{
9+
class Freq
10+
{
11+
int i;
12+
int c;
13+
}
14+
15+
private int[] buff = new int[256];
16+
/**
17+
* Main method
18+
* @param args
19+
* @throws Exception
20+
*/
21+
public static void main(String[] args) throws Exception
22+
{
23+
System.out.println(new SortCharByFrequency().frequencySort("askdfkasdkfasdkljfklasdjfkl"));
24+
}
25+
26+
public String frequencySort(String s)
27+
{
28+
if(s == null || s.isEmpty()) return s;
29+
Arrays.fill(buff, 0);
30+
StringBuilder sb = new StringBuilder();
31+
for(int i = 0, l = s.length(); i < l; i ++)
32+
buff[s.charAt(i)]++;
33+
34+
List<Freq> fList = new ArrayList<>();
35+
for(int i = 0; i < 256; i ++)
36+
{
37+
if(buff[i] > 0)
38+
{
39+
Freq f = new Freq();
40+
f.i = i;
41+
f.c = buff[i];
42+
fList.add(f);
43+
}
44+
}
45+
46+
Collections.sort(fList, (o1, o2) -> Integer.compare(o2.c, o1.c));
47+
48+
for(Freq f : fList)
49+
{
50+
char c = (char)f.i;
51+
int freq = f.c;
52+
while(freq-- > 0)
53+
sb.append(c);
54+
}
55+
return sb.toString();
56+
}
57+
}

problems/src/ThirdMaximumNumber.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import com.sun.tools.internal.ws.wsdl.document.soap.SOAPUse;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 25/03/2017.
5+
* Accepted
6+
*/
7+
public class ThirdMaximumNumber
8+
{
9+
/**
10+
* Main method
11+
* @param args
12+
* @throws Exception
13+
*/
14+
public static void main(String[] args) throws Exception
15+
{
16+
int[] a = {1, 2};
17+
System.out.println(new ThirdMaximumNumber().thirdMax(a));
18+
}
19+
20+
public int thirdMax(int[] nums)
21+
{
22+
long[] max = {Long.MIN_VALUE, Long.MIN_VALUE, Long.MIN_VALUE};
23+
int count = 0;
24+
for (int num : nums) {
25+
for (int j = 0; j < 3; j++) {
26+
if (max[j] > num) continue;
27+
else if (max[j] == num) break;
28+
int k = j;
29+
long temp1, temp2;
30+
temp1 = num;
31+
count++;
32+
while (k < 3) {
33+
temp2 = max[k];
34+
max[k] = temp1;
35+
temp1 = temp2;
36+
k++;
37+
}
38+
break;
39+
}
40+
}
41+
System.out.println(Integer.MIN_VALUE);
42+
return (count >= 3)? (int)max[2] : (int)max[0];
43+
}
44+
}

problems/src/WordLadderII.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/**
44
* Created by gouthamvidyapradhan on 24/03/2017.
5+
* Accepted
56
*/
67
public class WordLadderII
78
{

0 commit comments

Comments
 (0)