Advertisement
mjain

Misc Problems

May 1st, 2025 (edited)
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 23.92 KB | Source Code | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.HashMap;
  4. import java.util.List;
  5.  
  6. class Solution {
  7.     //You are given two integer arrays nums1 and nums2,
  8.     // sorted in non-decreasing order, and two integers m and n,
  9.     // representing the number of elements in nums1 and nums2 respectively.
  10.     public void merge(int[] nums1, int m, int[] nums2, int n) {
  11.         int l1 = m - 1;
  12.         int l2 = n - 1;
  13.         int i = l1 + l2 + 1;
  14.         while (l1 >= 0 && l2 >= 0) {
  15.             if (nums1[l1] > nums2[l2]) {
  16.                 nums1[i--] = nums1[l1--];
  17.             } else {
  18.                 nums1[i--] = nums2[l2--];
  19.             }
  20.         }
  21.         while (l2 >= 0) {
  22.             nums1[i--] = nums2[l2--];
  23.         }
  24.     }
  25.  
  26.     //Given an integer array nums and an integer val, remove all occurrences of val in nums in-place.
  27.     // The order of the elements may be changed.
  28.     // Then return the number of elements in nums which are not equal to val.
  29.     public int removeElement(int[] nums, int val) {
  30.         int n = nums.length;
  31.         int index = n - 1;
  32.         int count = 0;
  33.         for (int i = 0; i < n; i++) {
  34.             if (nums[i] == val) {
  35.                 while (index >= 0) {
  36.                     if (nums[index] == val) {
  37.                         nums[index] = Integer.MAX_VALUE;
  38.                         index--;
  39.                     } else if (nums[i] != Integer.MAX_VALUE) {
  40.                         nums[i] = nums[index];
  41.                         nums[index] = Integer.MAX_VALUE;
  42.                         index--;
  43.                         break;
  44.                     } else {
  45.                         index--;
  46.                     }
  47.                 }
  48.             }
  49.         }
  50.         for (int i = 0; i < n; i++) {
  51.             if (nums[i] != Integer.MAX_VALUE) {
  52.                 count++;
  53.             }
  54.         }
  55.         return count;
  56.     }
  57.     //Given an integer array nums sorted in non-decreasing order,
  58.     // remove the duplicates in-place such that each unique element appears only once.
  59.     // The relative order of the elements should be kept the same.
  60.     // Then return the number of unique elements in nums.
  61.  
  62.     public int removeDuplicates(int[] nums) {
  63.         int inderpos = 1;
  64.  
  65.         for (int i = 1; i < nums.length; i++) {
  66.             if (nums[i] != nums[i - 1]) {
  67.                 nums[inderpos++] = nums[i];
  68.             }
  69.         }
  70.         for (int j = inderpos; j < nums.length; j++) {
  71.             nums[j] = 0;
  72.         }
  73.  
  74.         return inderpos;
  75.     }
  76.  
  77.     //Given an integer array nums sorted in non-decreasing order,
  78.     // remove some duplicates in-place such that each unique element appears at most twice.
  79.     // The relative order of the elements should be kept the same.
  80.     //
  81.     //Since it is impossible to change the length of the array in some languages,
  82.     // you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.
  83.     public int removeDuplicatesV2(int[] nums) {
  84.         int inderpos = 1;
  85.         int count = 1;
  86.         for (int i = 1; i < nums.length; i++) {
  87.             if (nums[i] != nums[i - 1]) {
  88.                 nums[inderpos++] = nums[i];
  89.                 count = 1;
  90.             } else if (nums[i] == nums[i - 1] && count < 2) {
  91.                 count++;
  92.                 nums[inderpos++] = nums[i];
  93.             }
  94.         }
  95.         for (int j = inderpos; j < nums.length; j++) {
  96.             nums[j] = 0;
  97.         }
  98.         return inderpos;
  99.     }
  100.  
  101.     //Given an array nums of size n, return the majority element.
  102.     //
  103.     //The majority element is the element that appears more than ⌊n / 2⌋ times.
  104.     // You may assume that the majority element always exists in the array.
  105.     public int majorityElement(int[] nums) {
  106.         int votes = 0;
  107.         int candidate = -1;
  108.  
  109.         for (int i = 0; i < nums.length; i++) {
  110.             if (votes == 0) {
  111.                 candidate = nums[i];
  112.             }
  113.             if (nums[i] == candidate) {
  114.                 votes++;
  115.             } else {
  116.                 votes--;
  117.             }
  118.         }
  119.         return candidate;
  120.     }
  121.  
  122.     //Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
  123.     public void rotate(int[] nums, int k) {
  124.         int n = nums.length;
  125.         k = k % n;
  126.         int count = 0;
  127.  
  128.         for (int start = 0; count < n; start++) {
  129.             int current = start;
  130.             int prev = nums[start];
  131.  
  132.             do {
  133.                 int next = (current + k) % n;
  134.                 int temp = nums[next];
  135.                 nums[next] = prev;
  136.                 prev = temp;
  137.                 current = next;
  138.                 count++;
  139.             } while (start != current);
  140.         }
  141.     }
  142.  
  143.     //You are given an array prices where prices[i] is the price of a given stock on the ith day.
  144.     //
  145.     //You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
  146.     //
  147.     //Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
  148.     public int maxProfit(int[] prices) {
  149.         int minPrice = Integer.MAX_VALUE;
  150.         int maxProfit = 0;
  151.  
  152.         for (int price : prices) {
  153.             if (price < minPrice) {
  154.                 minPrice = price;
  155.             } else if (price - minPrice > maxProfit) {
  156.                 maxProfit = price - minPrice;
  157.             }
  158.         }
  159.  
  160.         return maxProfit;
  161.     }
  162.  
  163.     //You are given an integer array prices where prices[i] is the price of a given stock on the ith day.
  164.     //
  165.     //On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
  166.     //
  167.     //Find and return the maximum profit you can achieve.
  168.     public int maxProfitV2(int[] prices) {
  169.         int maxProfit = 0;
  170.  
  171.         for (int i = 1; i < prices.length; i++) {
  172.             if (prices[i - 1] < prices[i]) {
  173.                 maxProfit += prices[i] - prices[i - 1];
  174.             }
  175.         }
  176.  
  177.         return maxProfit;
  178.     }
  179.  
  180.     //You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.
  181.     //
  182.     //Return true if you can reach the last index, or false otherwise.
  183.     public boolean canJump(int[] nums) {
  184.         int max = 0;
  185.         for (int i = 0; i < nums.length; i++) {
  186.             if (i > max) {
  187.                 return false;
  188.             }
  189.             max = Math.max(max, i + nums[i]);
  190.         }
  191.  
  192.         return true;
  193.     }
  194.  
  195.     //ou are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0].
  196.     //
  197.     //Each element nums[i] represents the maximum length of a forward jump from index i. In other words, if you are at nums[i], you can jump to any nums[i + j]
  198.     public int jump(int[] nums) {
  199.         int jumps = 0;
  200.  
  201.         int fartherest = 0;
  202.         int currentIndex = 0;
  203.         for (int i = 0; i < nums.length - 1; i++) {
  204.             fartherest = Math.max(fartherest, i + nums[i]);
  205.             if (i == currentIndex) {
  206.                 jumps++;
  207.                 currentIndex = fartherest;
  208.             }
  209.         }
  210.         return jumps;
  211.     }
  212.  
  213.     //Given an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper, return the researcher's h-index.
  214.     //
  215.     //According to the definition of h-index on Wikipedia: The h-index is defined as the maximum value of h such that the given researcher has published at least h papers that have each been cited at least h times.
  216.     //
  217.     //
  218.     public int hIndex(int[] citations) {
  219.         Arrays.sort(citations);
  220.         int n = citations.length;
  221.  
  222.         for (int i = 0; i < n; i++) {
  223.             int h = n - i;
  224.             if (citations[i] >= h) {
  225.                 return h;
  226.             }
  227.         }
  228.  
  229.         return 0;
  230.     }
  231.  
  232.     //Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
  233.     //
  234.     //The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
  235.     //
  236.     //You must write an algorithm that runs in O(n) time and without using the division operation.
  237.     public int[] productExceptSelf(int[] nums) {
  238.         int length = nums.length;
  239.  
  240.         int[] answer = new int[length];
  241.         answer[0] = 1;
  242.         for (int i = 1; i < length; i++) {
  243.             answer[i] = nums[i - 1] * answer[i - 1];
  244.         }
  245.         int rightProduct = 1;
  246.         for (int i = length - 1; i >= 0; i--) {
  247.             answer[i] = answer[i] * rightProduct;
  248.             rightProduct = rightProduct * nums[i];
  249.         }
  250.         return answer;
  251.     }
  252.  
  253.     //There are n gas stations along a circular route, where the amount of gas at the ith station is gas[i].
  254.     //
  255.     //You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the ith station to its next (i + 1)th station. You begin the journey with an empty tank at one of the gas stations.
  256.     //
  257.     //Given two integer arrays gas and cost, return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1. If there exists a solution, it is guaranteed to be unique.
  258.     public int canCompleteCircuit(int[] gas, int[] cost) {
  259.         int totalTank = 0;
  260.         int currentTank = 0;
  261.         int startStation = 0;
  262.         for (int i = 0; i < gas.length; i++) {
  263.             totalTank += gas[i] - cost[i];
  264.             currentTank += gas[i] - cost[i];
  265.  
  266.             if (currentTank < 0) {
  267.                 currentTank = 0;
  268.                 startStation = i + 1;
  269.             }
  270.  
  271.         }
  272.         return totalTank >= 0 ? startStation : -1;
  273.     }
  274.  
  275.     //There are n children standing in a line. Each child is assigned a rating value given in the integer array ratings.
  276.     //
  277.     //You are giving candies to these children subjected to the following requirements:
  278.     //
  279.     //Each child must have at least one candy.
  280.     //Children with a higher rating get more candies than their neighbors.
  281.     public int candy(int[] ratings) {
  282.  
  283.         int n = ratings.length;
  284.         int[] candies = new int[n];
  285.  
  286.  
  287.         for (int i = 0; i < n; i++) {
  288.             candies[i] = 1;
  289.         }
  290.  
  291.  
  292.         for (int i = 1; i < n; i++) {
  293.             if (ratings[i] > ratings[i - 1]) {
  294.                 candies[i] = candies[i - 1] + 1;
  295.             }
  296.         }
  297.  
  298.         for (int i = n - 2; i >= 0; i--) {
  299.             if (ratings[i] > ratings[i + 1]) {
  300.                 candies[i] = Math.max(candies[i], candies[i + 1] + 1);
  301.             }
  302.         }
  303.  
  304.  
  305.         int totalCandies = 0;
  306.         for (int candy : candies) {
  307.             totalCandies += candy;
  308.         }
  309.  
  310.         return totalCandies;
  311.     }
  312.     //Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.
  313.  
  314.     public int trap(int[] height) {
  315.         int n = height.length;
  316.         int trappedWater = 0;
  317.         int leftMax = height[0];
  318.         int rightMax = height[n - 1];
  319.         int left = 1;
  320.         int right = n - 2;
  321.  
  322.         while (left <= right) {
  323.             if (leftMax >= rightMax) {
  324.                 trappedWater += Math.max(0, rightMax - height[right]);
  325.                 rightMax = Math.max(rightMax, height[right]);
  326.                 right--;
  327.  
  328.             } else {
  329.                 trappedWater += Math.max(0, leftMax - height[left]);
  330.                 leftMax = Math.max(leftMax, height[left]);
  331.                 left++;
  332.             }
  333.         }
  334.         return trappedWater;
  335.     }
  336.  
  337.     //Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
  338.     //
  339.     //Symbol       Value
  340.     //I             1
  341.     //V             5
  342.     //X             10
  343.     //L             50
  344.     //C             100
  345.     //D             500
  346.     //M             1000
  347.     public int romanToInt(String s) {
  348.         java.util.Map<Character, Integer> romanMap = new java.util.HashMap<>();
  349.         romanMap.put('I', 1);
  350.         romanMap.put('V', 5);
  351.         romanMap.put('X', 10);
  352.         romanMap.put('L', 50);
  353.         romanMap.put('C', 100);
  354.         romanMap.put('D', 500);
  355.         romanMap.put('M', 1000);
  356.  
  357.         int result = 0;
  358.         int prevValue = 0;
  359.  
  360.  
  361.         for (int i = s.length() - 1; i >= 0; i--) {
  362.             int currentValue = romanMap.get(s.charAt(i));
  363.             if (currentValue < prevValue) {
  364.                 result -= currentValue;
  365.             } else {
  366.                 result += currentValue;
  367.             }
  368.             prevValue = currentValue;
  369.         }
  370.  
  371.         return result;
  372.     }
  373.  
  374.     //Given an integer, convert it to a Roman numeral.
  375.     public String intToRoman(int num) {
  376.         StringBuilder sb = new StringBuilder();
  377.         int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
  378.         String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
  379.  
  380.         for (int i = 0; i < values.length; i++) {
  381.             while (num >= values[i]) {
  382.                 num -= values[i];
  383.                 sb.append(symbols[i]);
  384.             }
  385.  
  386.         }
  387.         return sb.toString();
  388.     }
  389.  
  390.     //Given a string s consisting of words and spaces, return the length of the last word in the string.
  391.     //
  392.     //A word is a maximal substring consisting of non-space characters only.
  393.     public int lengthOfLastWord(String s) {
  394.         int length = 0;
  395.         int i = s.length() - 1;
  396.  
  397.  
  398.         while (i >= 0 && s.charAt(i) == ' ') {
  399.             i--;
  400.         }
  401.  
  402.  
  403.         while (i >= 0 && s.charAt(i) != ' ') {
  404.             length++;
  405.             i--;
  406.         }
  407.  
  408.         return length;
  409.     }
  410.  
  411.     //Write a function to find the longest common prefix string amongst an array of strings.
  412.     //
  413.     //If there is no common prefix, return an empty string "".
  414.     public String longestCommonPrefix(String[] strs) {
  415.         if (strs == null || strs.length == 0) {
  416.             return "";
  417.         }
  418.         if (strs.length == 1) {
  419.             return strs[0];
  420.         }
  421.         String pref = strs[0];
  422.  
  423.         for (int i = 1; i < strs.length; i++) {
  424.             while (strs[i].indexOf(pref) != 0) {
  425.                 pref = pref.substring(0, pref.length() - 1);
  426.                 if (pref.isEmpty()) {
  427.                     return "";
  428.                 }
  429.             }
  430.         }
  431.  
  432.         return pref;
  433.     }
  434.  
  435.     //Given an input string s, reverse the order of the words.
  436.     //
  437.     //A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.
  438.     //
  439.     //Return a string of the words in reverse order concatenated by a single space.
  440.     public String reverseWords(String s) {
  441.         StringBuilder result = new StringBuilder();
  442.         int i = s.length() - 1;
  443.  
  444.         while (i >= 0) {
  445.  
  446.             while (i >= 0 && s.charAt(i) == ' ') {
  447.                 i--;
  448.             }
  449.             if (i < 0) break;
  450.  
  451.             int end = i;
  452.  
  453.             // Find the start of the word
  454.             while (i >= 0 && s.charAt(i) != ' ') {
  455.                 i--;
  456.             }
  457.             int start = i + 1;
  458.  
  459.  
  460.             result.append(s.substring(start, end + 1));
  461.             result.append(" ");
  462.         }
  463.  
  464.  
  465.         return result.toString().trim();
  466.     }
  467.  
  468.     //The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
  469.     public String convert(String s, int numRows) {
  470.         int length = s.length();
  471.         if (length <= numRows || numRows == 1) {
  472.             return s;
  473.         }
  474.  
  475.         StringBuilder[] rows = new StringBuilder[numRows];
  476.         for (int i = 0; i < numRows; i++) {
  477.             rows[i] = new StringBuilder();
  478.         }
  479.         int currentRow = 0;
  480.         boolean goingDown = false;
  481.         for (char c : s.toCharArray()) {
  482.             rows[currentRow].append(c);
  483.             if (currentRow == 0 || currentRow == numRows - 1) {
  484.                 goingDown = !goingDown;
  485.             }
  486.             if (goingDown) {
  487.                 currentRow++;
  488.             } else {
  489.                 currentRow--;
  490.             }
  491.         }
  492.  
  493.         StringBuilder result = new StringBuilder();
  494.         for (StringBuilder row : rows) {
  495.             result.append(row);
  496.         }
  497.  
  498.         return result.toString();
  499.     }
  500.  
  501.     //Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
  502.     public int strStr(String haystack, String needle) {
  503.         if (haystack == null || needle == null) {
  504.             return -1;
  505.         }
  506.         if (needle.length() == 0) {
  507.             return -1;
  508.         }
  509.  
  510.         int l1 = haystack.length();
  511.         int l2 = needle.length();
  512.  
  513.         for (int i = 0; i <= l1 - l2; i++) {
  514.             if (haystack.substring(i, i + l2).equalsIgnoreCase(needle)) {
  515.                 return i;
  516.             }
  517.         }
  518.  
  519.         return -1;
  520.     }
  521.  
  522.     //A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
  523.     public boolean isPalindrome(String s) {
  524.         if (s == null || s.length() == 1) {
  525.             return true;
  526.         }
  527.         StringBuilder sb = new StringBuilder();
  528.  
  529.         for (char c : s.toCharArray()) {
  530.             if (Character.isLetterOrDigit(c)) {
  531.                 sb.append(Character.toLowerCase(c));
  532.             }
  533.         }
  534.         String s1 = sb.toString();
  535.  
  536.         int left = 0, right = s1.length() - 1;
  537.         System.out.println(s1);
  538.         while (left < right) {
  539.             if (s1.charAt(left) != s1.charAt(right)) {
  540.                 return false;
  541.             }
  542.             left++;
  543.             right--;
  544.         }
  545.  
  546.         return true;
  547.     }
  548.  
  549.     //Given two strings s and t, return true if s is a subsequence of t, or false otherwise.
  550.     //
  551.     //A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).
  552.     public boolean isSubsequence(String s, String t) {
  553.         int i = 0, j = 0;
  554.  
  555.         while (i < s.length() && j < t.length()) {
  556.             if (s.charAt(i) == t.charAt(j)) {
  557.                 i++;
  558.             }
  559.             j++;
  560.         }
  561.  
  562.         return i == s.length();
  563.     }
  564.  
  565.     //Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length.
  566.     public int[] twoSum(int[] numbers, int target) {
  567.         if (numbers == null || numbers.length == 1) {
  568.             return null;
  569.         }
  570.         int left = 0;
  571.         int right = numbers.length - 1;
  572.  
  573.         while (left < right) {
  574.             int sum = numbers[left] + numbers[right];
  575.             if (sum == target) {
  576.                 return new int[]{left + 1, right + 1};
  577.             }
  578.             if (sum > target) {
  579.                 right--;
  580.             }
  581.             if (sum < target) {
  582.                 left++;
  583.             }
  584.         }
  585.  
  586.  
  587.         return null;
  588.     }
  589.  
  590.     //You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
  591. //
  592. //Find two lines that together with the x-axis form a container, such that the container contains the most water.
  593. //
  594. //Return the maximum amount of water a container can store.
  595.     public int maxArea(int[] height) {
  596.  
  597.  
  598.         int water = 0;
  599.         int left = 0;
  600.         int right = height.length - 1;
  601.  
  602.         while (left < right) {
  603.             int currentWater = Math.min(height[right], height[left]) * (right - left);
  604.             water = Math.max(currentWater, water);
  605.             if (height[left] < height[right]) {
  606.                 left++;
  607.             } else {
  608.                 right--;
  609.             }
  610.  
  611.         }
  612.         return water;
  613.     }
  614.  
  615.     //Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
  616. //
  617. //Notice that the solution set must not contain duplicate triplets.
  618.     public List<List<Integer>> threeSum(int[] nums) {
  619.         List<List<Integer>> list = new ArrayList<>();
  620.         Arrays.sort(nums);
  621.  
  622.         int length = nums.length;
  623.         for (int i = 0; i < length - 2; i++) {
  624.             if (i > 0 && nums[i] == nums[i - 1]) {
  625.                 continue;
  626.             }
  627.  
  628.             int left = i + 1;
  629.             int right = length - 1;
  630.             while (left < right) {
  631.                 int sum = nums[i] + nums[left] + nums[right];
  632.                 if (sum == 0) {
  633.                     list.add(Arrays.asList(nums[i], nums[left], nums[right]));
  634.                     while (left < right && nums[left] == nums[left + 1]) left++;
  635.                     while (left < right && nums[right] == nums[right - 1]) right--;
  636.                     left++;
  637.                     right--;
  638.                 } else if (sum < 0) {
  639.                     left++;
  640.                 } else {
  641.                     right--;
  642.                 }
  643.  
  644.             }
  645.  
  646.         }
  647.         return list;
  648.     }
  649.  
  650.     //Given an array of positive integers nums and a positive integer target,
  651.     // return the minimal length of a subarray whose sum is greater than or equal to target.
  652.     // If there is no such subarray, return 0 instead.
  653.     public int minSubArrayLen(int target, int[] nums) {
  654.         if (nums == null || nums.length == 0) {
  655.             return 0;
  656.         }
  657.         if (nums.length == 1) {
  658.             if (nums[0] >= target) {
  659.                 return 1;
  660.             }
  661.         }
  662.  
  663.  
  664.         int ans = Integer.MAX_VALUE;
  665.         int sum = 0;
  666.         int start = 0;
  667.  
  668.  
  669.         for (int end = 0; end < nums.length; end++) {
  670.             sum += nums[end];
  671.             while (sum >= target) {
  672.                 ans = Math.min(ans, end - start + 1);
  673.                 sum -= nums[start];
  674.                 start++;
  675.             }
  676.         }
  677.  
  678.  
  679.         if (ans == Integer.MAX_VALUE) return 0;
  680.         return ans;
  681.     }
  682.  
  683.     //Given a string s, find the length of the longest substring without duplicate characters.
  684.     public int lengthOfLongestSubstring(String s) {
  685.         if (s == null || s.length() == 0) {
  686.             return 0;
  687.         }
  688.         if (s.length() == 1) {
  689.             return 1;
  690.         }
  691.         int left = 0;
  692.         int right = 0;
  693.  
  694.         HashMap<Integer, Boolean> vMap = new HashMap();
  695.  
  696.         int maxWindow = 1;
  697.         while (right < s.length()) {
  698.             while (vMap.get(s.charAt(right) - 'a') != null && vMap.get(s.charAt(right) - 'a') == true) {
  699.                 int i = s.charAt(left) - 'a';
  700.                 vMap.put(i, false);
  701.                 left++;
  702.             }
  703.             vMap.put(s.charAt(right) - 'a', true);
  704.  
  705.             maxWindow = Math.max(right - left + 1, maxWindow);
  706.             right++;
  707.         }
  708.         return maxWindow;
  709.     }
  710.  
  711.  
  712.     public static void main(String[] args) {
  713.  
  714.         Solution s = new Solution();
  715.         int length = s.lengthOfLongestSubstring("au");
  716.         System.out.println(length);
  717.     }
  718.  
  719.     private static void printArray(int[] result) {
  720.         for (int i = 0; i < result.length; i++) {
  721.             System.out.print(result[i] + " ");
  722.         }
  723.     }
  724. }
  725.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement