Skip to content

Commit 64c3ea7

Browse files
author
Roy
committed
Fixing tabs
1 parent 2227403 commit 64c3ea7

10 files changed

Lines changed: 613 additions & 613 deletions

‎src/com/interview/string/LongPanlindromeSubstring.java‎

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,74 +6,74 @@
66
*/
77
public class LongPanlindromeSubstring {
88

9-
public int longestPalindromeSubstringEasy(char arr[]) {
9+
public int longestPalindromeSubstringEasy(char arr[]) {
1010

11-
int longest_substring = 1;
12-
for (int i = 0; i < arr.length; i++) {
11+
int longest_substring = 1;
12+
for (int i = 0; i < arr.length; i++) {
1313

14-
int x, y;
15-
int palindrome;
16-
x = i;
17-
y = i + 1;
18-
palindrome = 0;
19-
while (x >= 0 && y < arr.length && arr[x] == arr[y]) {
20-
x--;
21-
y++;
22-
palindrome += 2;
23-
}
24-
longest_substring = Math.max(longest_substring, palindrome);
25-
26-
x = i - 1;
27-
y = i + 1;
28-
palindrome = 1;
29-
while (x >= 0 && y < arr.length && arr[x] == arr[y]) {
30-
x--;
31-
y++;
32-
palindrome += 2;
33-
}
34-
longest_substring = Math.max(longest_substring, palindrome);
35-
}
36-
return longest_substring;
37-
}
38-
39-
public int longestPalindromeDynamic(char []str){
40-
boolean T[][] = new boolean[str.length][str.length];
41-
42-
for(int i=0; i < T.length; i++){
43-
T[i][i] = true;
44-
}
45-
46-
int max = 1;
47-
for(int l = 2; l <= str.length; l++){
48-
int len = 0;
49-
for(int i=0; i < str.length-l+1; i++){
50-
int j = i + l-1;
51-
len = 0;
52-
if(l == 2){
53-
if(str[i] == str[j]){
54-
T[i][j] = true;
55-
len = 2;
56-
}
57-
}else{
58-
if(str[i] == str[j] && T[i+1][j-1]){
59-
T[i][j] = true;
60-
len = j -i + 1;
61-
}
62-
}
63-
if(len > max){
64-
max = len;
65-
}
66-
}
67-
}
68-
return max;
69-
}
14+
int x, y;
15+
int palindrome;
16+
x = i;
17+
y = i + 1;
18+
palindrome = 0;
19+
while (x >= 0 && y < arr.length && arr[x] == arr[y]) {
20+
x--;
21+
y++;
22+
palindrome += 2;
23+
}
24+
longest_substring = Math.max(longest_substring, palindrome);
25+
26+
x = i - 1;
27+
y = i + 1;
28+
palindrome = 1;
29+
while (x >= 0 && y < arr.length && arr[x] == arr[y]) {
30+
x--;
31+
y++;
32+
palindrome += 2;
33+
}
34+
longest_substring = Math.max(longest_substring, palindrome);
35+
}
36+
return longest_substring;
37+
}
38+
39+
public int longestPalindromeDynamic(char []str){
40+
boolean T[][] = new boolean[str.length][str.length];
41+
42+
for(int i=0; i < T.length; i++){
43+
T[i][i] = true;
44+
}
45+
46+
int max = 1;
47+
for(int l = 2; l <= str.length; l++){
48+
int len = 0;
49+
for(int i=0; i < str.length-l+1; i++){
50+
int j = i + l-1;
51+
len = 0;
52+
if(l == 2){
53+
if(str[i] == str[j]){
54+
T[i][j] = true;
55+
len = 2;
56+
}
57+
}else{
58+
if(str[i] == str[j] && T[i+1][j-1]){
59+
T[i][j] = true;
60+
len = j -i + 1;
61+
}
62+
}
63+
if(len > max){
64+
max = len;
65+
}
66+
}
67+
}
68+
return max;
69+
}
7070

71-
public static void main(String args[]) {
71+
public static void main(String args[]) {
7272

73-
char[] str = { 'a', 'b', 'a', 'e', 'a', 'b','a' };
74-
LongPanlindromeSubstring lps = new LongPanlindromeSubstring();
75-
int result = lps.longestPalindromeDynamic(str);
76-
System.out.print(result);
77-
}
73+
char[] str = { 'a', 'b', 'a', 'e', 'a', 'b','a' };
74+
LongPanlindromeSubstring lps = new LongPanlindromeSubstring();
75+
int result = lps.longestPalindromeDynamic(str);
76+
System.out.print(result);
77+
}
7878

7979
}

‎src/com/interview/string/LongestSubstringWithoutRepetingCharacter.java‎

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,35 @@
1212
*/
1313
public class LongestSubstringWithoutRepetingCharacter {
1414

15-
public int longestSubstring(char str[]){
16-
int maxLen = -1 ;
17-
int len =0;
18-
int start=0;
19-
Set<Character> visited = new HashSet<Character>();
20-
for(int i=0; i < str.length; i++){
21-
//if visited contains current character
22-
//remove all characters from start till previous version of current
23-
//character if found.
24-
if(visited.contains(str[i])){
25-
if(len > maxLen){
26-
maxLen = len;
27-
}
28-
do{
29-
visited.remove(str[start]);
30-
start++;
31-
len--;
32-
}while(str[start-1] != str[i]);
33-
}
34-
//this needs to be done always no matter character is found in visisted
35-
//or not
36-
visited.add(str[i]);
37-
len++;
38-
}
39-
return Math.max(len, maxLen);
40-
}
41-
42-
public static void main(String args[]){
43-
LongestSubstringWithoutRepetingCharacter lsw = new LongestSubstringWithoutRepetingCharacter();
44-
System.out.println(lsw.longestSubstring("ABCDECAMNCZB".toCharArray()));
45-
}
15+
public int longestSubstring(char str[]){
16+
int maxLen = -1 ;
17+
int len =0;
18+
int start=0;
19+
Set<Character> visited = new HashSet<Character>();
20+
for(int i=0; i < str.length; i++){
21+
//if visited contains current character
22+
//remove all characters from start till previous version of current
23+
//character if found.
24+
if(visited.contains(str[i])){
25+
if(len > maxLen){
26+
maxLen = len;
27+
}
28+
do{
29+
visited.remove(str[start]);
30+
start++;
31+
len--;
32+
}while(str[start-1] != str[i]);
33+
}
34+
//this needs to be done always no matter character is found in visisted
35+
//or not
36+
visited.add(str[i]);
37+
len++;
38+
}
39+
return Math.max(len, maxLen);
40+
}
41+
42+
public static void main(String args[]){
43+
LongestSubstringWithoutRepetingCharacter lsw = new LongestSubstringWithoutRepetingCharacter();
44+
System.out.println(lsw.longestSubstring("ABCDECAMNCZB".toCharArray()));
45+
}
4646
}
Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,72 @@
11
package com.interview.string;
22

33
/**
4-
.Given an input string S write a function which returns true if it satisfies S = nT. Basically you have to find if a given string can be represented from a substring by iterating it ÒnÓ times. n >= 2
5-
An example would suffice Ð
4+
.Given an input string S write a function which returns true if it satisfies S = nT. Basically you have to find if a given string can be represented from a substring by iterating it �n� times. n >= 2
5+
An example would suffice �
66
Function should return true if
7-
1) S = ÒababÓ
8-
2) S = ÒabcdabcdÓ
9-
3) S = ÒabcabcabcÓ
10-
4) S = ÒzzxzzxzzxÓ
7+
1) S = �abab�
8+
2) S = �abcdabcd�
9+
3) S = �abcabcabc�
10+
4) S = �zzxzzxzzx�
1111
1212
Function should return false if
13-
1) S = ÒabacÓ
14-
2) S = ÒabcdabbdÓ
15-
3) S = ÒabcabcefgÓ
16-
4) S = ÒzzxzzyzzxÓ
13+
1) S = �abac�
14+
2) S = �abcdabbd�
15+
3) S = �abcabcefg�
16+
4) S = �zzxzzyzzx�
1717
*/
1818
public class NTMatch {
1919

20-
public boolean match(char str[]){
21-
int kmp[] = buildKMP(str);
22-
int index = kmp[str.length-1];
23-
int end = str.length - index-1;
24-
if(end >= str.length/2){
25-
return false;
26-
}
27-
int j = end+1;
28-
int i = 0;
29-
while(j < str.length){
30-
if(str[i] != str[j]){
31-
return false;
32-
}
33-
i = (i+1)%(end+1);
34-
j++;
35-
}
36-
37-
if(i == 0){
38-
return true;
39-
}
40-
return false;
41-
}
42-
43-
private int[] buildKMP(char str[]){
20+
public boolean match(char str[]){
21+
int kmp[] = buildKMP(str);
22+
int index = kmp[str.length-1];
23+
int end = str.length - index-1;
24+
if(end >= str.length/2){
25+
return false;
26+
}
27+
int j = end+1;
28+
int i = 0;
29+
while(j < str.length){
30+
if(str[i] != str[j]){
31+
return false;
32+
}
33+
i = (i+1)%(end+1);
34+
j++;
35+
}
36+
37+
if(i == 0){
38+
return true;
39+
}
40+
return false;
41+
}
42+
43+
private int[] buildKMP(char str[]){
4444

45-
int result[] = new int[str.length];
46-
47-
int i =1;
48-
result[0] = 0;
49-
int len =0;
50-
while(i < str.length){
51-
if(str[i] == str[len]){
52-
len++;
53-
result[i] = len;
54-
i++;
55-
}else{
56-
if(len != 0){
57-
len = result[len-1];
58-
}else{
59-
len =0;
60-
result[i] = 0;
61-
i++;
62-
}
63-
}
64-
}
65-
return result;
66-
}
67-
68-
public static void main(String args[]){
69-
NTMatch ntMatch = new NTMatch();
70-
System.out.println(ntMatch.match("bababababa".toCharArray()));
71-
}
45+
int result[] = new int[str.length];
46+
47+
int i =1;
48+
result[0] = 0;
49+
int len =0;
50+
while(i < str.length){
51+
if(str[i] == str[len]){
52+
len++;
53+
result[i] = len;
54+
i++;
55+
}else{
56+
if(len != 0){
57+
len = result[len-1];
58+
}else{
59+
len =0;
60+
result[i] = 0;
61+
i++;
62+
}
63+
}
64+
}
65+
return result;
66+
}
67+
68+
public static void main(String args[]){
69+
NTMatch ntMatch = new NTMatch();
70+
System.out.println(ntMatch.match("bababababa".toCharArray()));
71+
}
7272
}

0 commit comments

Comments
 (0)