From 2e09e44a38d9184f312d69f1ae046f5addb6ef1f Mon Sep 17 00:00:00 2001 From: SanOtaku Date: Thu, 26 May 2022 15:17:23 +0530 Subject: [PATCH] Add ZigZag Encoding and Longest Nonrepetitive Substring Algorithms (#3058) --- .../strings/longestNonRepeativeSubstring.java | 47 +++++++++++++++++++ .../strings/zigZagPattern/README.md | 36 ++++++++++++++ .../strings/zigZagPattern/zigZagPattern.java | 30 ++++++++++++ .../longestNonRepeativeSubstringTest.java | 14 ++++++ .../zigZagPattern/zigZagPatternTest.java | 14 ++++++ 5 files changed, 141 insertions(+) create mode 100644 src/main/java/com/thealgorithms/strings/longestNonRepeativeSubstring.java create mode 100644 src/main/java/com/thealgorithms/strings/zigZagPattern/README.md create mode 100644 src/main/java/com/thealgorithms/strings/zigZagPattern/zigZagPattern.java create mode 100644 src/test/java/com/thealgorithms/strings/longestNonRepeativeSubstringTest.java create mode 100644 src/test/java/com/thealgorithms/strings/zigZagPattern/zigZagPatternTest.java diff --git a/src/main/java/com/thealgorithms/strings/longestNonRepeativeSubstring.java b/src/main/java/com/thealgorithms/strings/longestNonRepeativeSubstring.java new file mode 100644 index 000000000..8cb456ae5 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/longestNonRepeativeSubstring.java @@ -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 ; + } + +} diff --git a/src/main/java/com/thealgorithms/strings/zigZagPattern/README.md b/src/main/java/com/thealgorithms/strings/zigZagPattern/README.md new file mode 100644 index 000000000..17e5ba6e2 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/zigZagPattern/README.md @@ -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 \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/strings/zigZagPattern/zigZagPattern.java b/src/main/java/com/thealgorithms/strings/zigZagPattern/zigZagPattern.java new file mode 100644 index 000000000..e0e019706 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/zigZagPattern/zigZagPattern.java @@ -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 ) ; + } + +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/strings/longestNonRepeativeSubstringTest.java b/src/test/java/com/thealgorithms/strings/longestNonRepeativeSubstringTest.java new file mode 100644 index 000000000..f8bc63562 --- /dev/null +++ b/src/test/java/com/thealgorithms/strings/longestNonRepeativeSubstringTest.java @@ -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 ) ; + } +} diff --git a/src/test/java/com/thealgorithms/strings/zigZagPattern/zigZagPatternTest.java b/src/test/java/com/thealgorithms/strings/zigZagPattern/zigZagPatternTest.java new file mode 100644 index 000000000..6c0e0333b --- /dev/null +++ b/src/test/java/com/thealgorithms/strings/zigZagPattern/zigZagPatternTest.java @@ -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" ) ; + } +}