Skip to content

Commit 2e09e44

Browse files
authored
Add ZigZag Encoding and Longest Nonrepetitive Substring Algorithms (TheAlgorithms#3058)
1 parent f35eef2 commit 2e09e44

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.thealgorithms.strings ;
2+
import java.util.HashMap ;
3+
class longestNonRepeativeSubstring {
4+
5+
public static int lengthOfLongestSubstring(String s) {
6+
7+
int max = 0 , start = 0 , i = 0 ;
8+
HashMap< Character , Integer > map = new HashMap<>() ;
9+
10+
while ( i < s.length() ) {
11+
12+
char temp = s.charAt( i ) ;
13+
14+
// adding key to map if not present
15+
if ( ! map.containsKey( temp ) )
16+
map.put( temp , 0 ) ;
17+
18+
// checking if the first value is the dublicate value
19+
else if ( s.charAt( start ) == temp )
20+
start++ ;
21+
22+
// checking if the previous value is dublicate value
23+
else if ( s.charAt( i - 1 ) == temp ) {
24+
if ( max < map.size() ) max = map.size() ;
25+
map = new HashMap<>() ;
26+
start = i ;
27+
i-- ;
28+
}
29+
30+
// last possible place where dublicate value can be is between start and i
31+
else {
32+
if ( max < map.size() ) max = map.size() ;
33+
while ( s.charAt( start ) != temp ) {
34+
map.remove( s.charAt( start ) ) ;
35+
start++ ;
36+
}
37+
start++ ;
38+
}
39+
40+
i++ ;
41+
42+
}
43+
if ( max < map.size() ) max = map.size() ;
44+
return max ;
45+
}
46+
47+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# About
2+
3+
convert string into a zig-zagged string
4+
5+
for example :
6+
7+
string = "123456789" , numRows = 3
8+
ans = "159246837"
9+
explanation
10+
1 5 9
11+
2 4 6 8
12+
3 7
13+
14+
string = "HelloWorldKotlin" , k = 4
15+
ans = "HoteWrollolKildn"
16+
explanation
17+
H o t
18+
e W r o l
19+
l o l K i
20+
l d n
21+
22+
# working
23+
24+
if string size is smaller than numRows or numRows is smaller than 2
25+
than we can return string because it will make no changes to string.
26+
If not than
27+
we initiate three variable depth which is equalvalent to numRows ,
28+
height which starts with 1 and start with starting index of string.
29+
than we generate spaces to skip using formula 2 + (( n - 1 ) * 2 )
30+
for both height and depth
31+
with each iteration we decrement depth and increate height and start
32+
by one also we keep contantating character on to new string with first
33+
depth spaces and later height spaces that we generated using formula
34+
if not zero
35+
36+
# beats 80% of submission on leetcode
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.thealgorithms.strings.zigZagPattern;
2+
class zigZagPattern {
3+
4+
public static String encode(String s, int numRows) {
5+
if ( numRows < 2 || s.length() < numRows ) return s ;
6+
int start = 0 , index = 0 , height = 1 , depth = numRows ;
7+
char[] zigZagedArray = new char[ s.length() ] ;
8+
while ( depth != 0 ) {
9+
int pointer = start , height_space = 2 + ( ( height - 2 ) * 2 ) , depth_space = 2 + ( ( depth - 2 ) * 2 ) ;
10+
boolean bool = true ;
11+
while ( pointer < s.length() ) {
12+
zigZagedArray[index++] = s.charAt( pointer ) ;
13+
if ( height_space == 0 ) pointer += depth_space ;
14+
else if ( depth_space == 0 ) pointer += height_space ;
15+
else if ( bool ) {
16+
pointer += depth_space ;
17+
bool = false ;
18+
} else {
19+
pointer += height_space ;
20+
bool = true ;
21+
}
22+
}
23+
height++ ;
24+
depth-- ;
25+
start++ ;
26+
}
27+
return new String( zigZagedArray ) ;
28+
}
29+
30+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.thealgorithms.strings;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class longestNonRepeativeSubstringTest {
7+
@Test
8+
public void palindrome() {
9+
String input1 = "HelloWorld";
10+
String input2 = "javaIsAProgrammingLanguage";
11+
Assertions.assertEquals( longestNonRepeativeSubstring.lengthOfLongestSubstring( input1 ) , 5 ) ;
12+
Assertions.assertEquals( longestNonRepeativeSubstring.lengthOfLongestSubstring( input2 ) , 9 ) ;
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.thealgorithms.strings.zigZagPattern;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class zigZagPatternTest {
7+
@Test
8+
public void palindrome() {
9+
String input1 = "HelloWorldFromJava";
10+
String input2 = "javaIsAProgrammingLanguage";
11+
Assertions.assertEquals( zigZagPattern.encode( input1 , 4 ) , "HooeWrrmalolFJvlda" ) ;
12+
Assertions.assertEquals( zigZagPattern.encode( input2 , 4 ) , "jAaLgasPrmgaaevIrgmnnuaoig" ) ;
13+
}
14+
}

0 commit comments

Comments
 (0)