Repository: TheAlgorithms/Java
Pull request #3058 (2022-05-22 11:52:35)

new algo
### **Describe your change:**
two new algo in com.thealgorithm.strings
1. longest non repeative substing
2. zig zag pattern

- [x] Add an algorithm?
- [ ] Fix a bug or typo in an existing algorithm?
- [ ] Documentation change?

#### References

<!-- Add any reference to previous pull-request or issue -->

### **Checklist:**

<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [x] All filenames are in all uppercase characters with no spaces or dashes.
- [x] All functions and variable names follow Java naming conventions.
- [ ] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
- [ ] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.


SanOtaku09: > Please add tests, format code as in other files in the repo, and give descriptive names to the classes and functions instead of `Solution` and `convert`

renamed the class name and variable 
added test for both file

Patch:
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 00000000..8cb456ae
--- /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 00000000..17e5ba6e
--- /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 00000000..e0e01970
--- /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


Test patch:
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 00000000..f8bc6356
--- /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 00000000..6c0e0333
--- /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" ) ;
+    }
+}