mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 01:35:16 +08:00
Add ZigZag Encoding and Longest Nonrepetitive Substring Algorithms (#3058)
This commit is contained in:
@ -0,0 +1,47 @@
|
||||
package com.thealgorithms.strings ;
|
||||
import java.util.HashMap ;
|
||||
class longestNonRepeativeSubstring {
|
||||
|
||||
public static int lengthOfLongestSubstring(String s) {
|
||||
|
||||
int max = 0 , start = 0 , i = 0 ;
|
||||
HashMap< Character , Integer > map = new HashMap<>() ;
|
||||
|
||||
while ( i < s.length() ) {
|
||||
|
||||
char temp = s.charAt( i ) ;
|
||||
|
||||
// adding key to map if not present
|
||||
if ( ! map.containsKey( temp ) )
|
||||
map.put( temp , 0 ) ;
|
||||
|
||||
// checking if the first value is the dublicate value
|
||||
else if ( s.charAt( start ) == temp )
|
||||
start++ ;
|
||||
|
||||
// checking if the previous value is dublicate value
|
||||
else if ( s.charAt( i - 1 ) == temp ) {
|
||||
if ( max < map.size() ) max = map.size() ;
|
||||
map = new HashMap<>() ;
|
||||
start = i ;
|
||||
i-- ;
|
||||
}
|
||||
|
||||
// last possible place where dublicate value can be is between start and i
|
||||
else {
|
||||
if ( max < map.size() ) max = map.size() ;
|
||||
while ( s.charAt( start ) != temp ) {
|
||||
map.remove( s.charAt( start ) ) ;
|
||||
start++ ;
|
||||
}
|
||||
start++ ;
|
||||
}
|
||||
|
||||
i++ ;
|
||||
|
||||
}
|
||||
if ( max < map.size() ) max = map.size() ;
|
||||
return max ;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
# About
|
||||
|
||||
convert string into a zig-zagged string
|
||||
|
||||
for example :
|
||||
|
||||
string = "123456789" , numRows = 3
|
||||
ans = "159246837"
|
||||
explanation
|
||||
1 5 9
|
||||
2 4 6 8
|
||||
3 7
|
||||
|
||||
string = "HelloWorldKotlin" , k = 4
|
||||
ans = "HoteWrollolKildn"
|
||||
explanation
|
||||
H o t
|
||||
e W r o l
|
||||
l o l K i
|
||||
l d n
|
||||
|
||||
# working
|
||||
|
||||
if string size is smaller than numRows or numRows is smaller than 2
|
||||
than we can return string because it will make no changes to string.
|
||||
If not than
|
||||
we initiate three variable depth which is equalvalent to numRows ,
|
||||
height which starts with 1 and start with starting index of string.
|
||||
than we generate spaces to skip using formula 2 + (( n - 1 ) * 2 )
|
||||
for both height and depth
|
||||
with each iteration we decrement depth and increate height and start
|
||||
by one also we keep contantating character on to new string with first
|
||||
depth spaces and later height spaces that we generated using formula
|
||||
if not zero
|
||||
|
||||
# beats 80% of submission on leetcode
|
@ -0,0 +1,30 @@
|
||||
package com.thealgorithms.strings.zigZagPattern;
|
||||
class zigZagPattern {
|
||||
|
||||
public static String encode(String s, int numRows) {
|
||||
if ( numRows < 2 || s.length() < numRows ) return s ;
|
||||
int start = 0 , index = 0 , height = 1 , depth = numRows ;
|
||||
char[] zigZagedArray = new char[ s.length() ] ;
|
||||
while ( depth != 0 ) {
|
||||
int pointer = start , height_space = 2 + ( ( height - 2 ) * 2 ) , depth_space = 2 + ( ( depth - 2 ) * 2 ) ;
|
||||
boolean bool = true ;
|
||||
while ( pointer < s.length() ) {
|
||||
zigZagedArray[index++] = s.charAt( pointer ) ;
|
||||
if ( height_space == 0 ) pointer += depth_space ;
|
||||
else if ( depth_space == 0 ) pointer += height_space ;
|
||||
else if ( bool ) {
|
||||
pointer += depth_space ;
|
||||
bool = false ;
|
||||
} else {
|
||||
pointer += height_space ;
|
||||
bool = true ;
|
||||
}
|
||||
}
|
||||
height++ ;
|
||||
depth-- ;
|
||||
start++ ;
|
||||
}
|
||||
return new String( zigZagedArray ) ;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.thealgorithms.strings;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class longestNonRepeativeSubstringTest {
|
||||
@Test
|
||||
public void palindrome() {
|
||||
String input1 = "HelloWorld";
|
||||
String input2 = "javaIsAProgrammingLanguage";
|
||||
Assertions.assertEquals( longestNonRepeativeSubstring.lengthOfLongestSubstring( input1 ) , 5 ) ;
|
||||
Assertions.assertEquals( longestNonRepeativeSubstring.lengthOfLongestSubstring( input2 ) , 9 ) ;
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.thealgorithms.strings.zigZagPattern;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class zigZagPatternTest {
|
||||
@Test
|
||||
public void palindrome() {
|
||||
String input1 = "HelloWorldFromJava";
|
||||
String input2 = "javaIsAProgrammingLanguage";
|
||||
Assertions.assertEquals( zigZagPattern.encode( input1 , 4 ) , "HooeWrrmalolFJvlda" ) ;
|
||||
Assertions.assertEquals( zigZagPattern.encode( input2 , 4 ) , "jAaLgasPrmgaaevIrgmnnuaoig" ) ;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user