## Java: Find the longest substring without any number and at least one upper case character

longest substring without repeating characters - youtube

longest substring with repeating characters java

longest substring without repeating characters - leetcode

longest substring without repeating characters c#

maximum substring hackerrank solution

find the length of the longest substring without repeating characters c#

programcreek longest substring without repeating characters

Came across a programming exercise and was stuck. The problem is:

You need to define a valid password for an email but the only restrictions are:

The password must contain one uppercase character

The password should not have numeric digit

Now, given a String, find the length of the longest substring which is a valid password. For e.g

`Input Str = "a0Ba"`

, the output should be 2 as "Ba" is the valid substring.

I used the concept of longest substring without repeating characters which I already did before but was unable to modify it to find the solution to above problem. My code for longest substring without repeating characters is:

public int lengthOfLongestSubstring(String s) { int n = s.length(); Set<Character> set = new HashSet<>(); int ans = 0, i = 0, j = 0; while (i < n && j < n) { // try to extend the range [i, j] if (!set.contains(s.charAt(j))){ set.add(s.charAt(j++)); ans = Math.max(ans, j - i); } else { set.remove(s.charAt(i++)); } } return ans; }

How about

final String input = "a0Ba"; final int answer = Arrays.stream(input.split("[0-9]+")) .filter(s -> s.matches("(.+)?[A-Z](.+)?")) .sorted((s1, s2) -> s2.length() - s1.length()) .findFirst() .orElse("") .length(); out.println(answer);

`Arrays.stream(input.split("[0-9]+"))`

splits the original string into an array of strings. The separator is any sequence of numbers (numbers aren't allowed so they serve as separators). Then, a stream is created so I can apply functional operations and transformations.

`.filter(s -> s.matches("(.+)?[A-Z](.+)?"))`

keeps into the stream only strings that have at least one upper-case letter.

`.sorted((s1, s2) -> s2.length() - s1.length())`

sorts the stream by length (desc).

`.findFirst()`

tries to get the first string of the stream.

`.orElse("")`

returns an empty string if no string was found.

`.length();`

gets the length of the string.

The password must contain one uppercase character and the password should not have numeric digit. From there on you can check for string lengths for longest sub-string. + "` having length " + len); } else { alert('No substring found. I've literally translated your Java code into Javascript code, but I'm Now, given a String, find the length of the longest substring which is a valid password. For e.g Input Str = "a0Ba" , the output should be 2 as “Ba” is the valid substring. Below is the step by step code with testcase console output: (The code is bit lengthy, but self explanatory)

I suggest that you split your String to have an array of strings without digit:

yourString.split("[0-9]")

Then iterate over this array (says array a) to get the longest string that contains one Upper case character:

a[i].matches("[a-z]*[A-Z]{1}[a-z]*");

How do you find the length of the longest substring without repeating characters? LeetCode – Longest Substring Without Repeating Characters (Java) Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

You can use a simple array. The algorithm to use would be a dynamic sliding window. Here is an example of a static sliding window: What is a Sliding Window

**The algorithm should be as follows:**

Keep track of 2 indexes of the array of `char`

. These 2 indexes will be referred to as `front`

and `back`

here, representing the front and back of the array.

Have an `int`

(I'll name it `up`

here) to keep track of the number of upper case `char`

.

Set all to 0.

Use a while loop that terminates if `front > N`

where `N`

is the number of `char`

given.

If the next char is not a number, add 1 to `front`

. Then check if that `char`

is upper case. If so, add 1 to `up`

.

If `up`

is at least 1, update the maximum length if necessary.

If the next char is a number, continue checking the following `char`

if they are also numbers. Set `front`

to the first index where the `char`

is not a number and `back`

to `front-1`

.

Output the maximum length.

How do you find the common substring of two strings? Length of the longest substring without repeating characters. Given a string str, find the length of the longest substring without repeating characters. For “ABDEFGABEF”, the longest substring are “BDEFGA” and “DEFGAB”, with length 6. For “BBBB” the longest substring is “B”, with length 1. For “GEEKSFORGEEKS”, there are two longest substrings shown in the below diagrams, with length 7.

You can use my solution which runs in O(n) time and finds the longest part without any digit and with a capital letter:

String testString = "skjssldfkjsakdfjlskdssfkjslakdfiop7adfaijsldifjasdjfil8klsasdfŞdijpfjapodifjpoaidjfpoaidjpfi9a"; int startIndex = 0; int longestStartIndex = 0; int endIndex = 0; int index = 0; int longestLength = Integer.MIN_VALUE; boolean foundUpperCase = false; while(index <= testString.length()) { if (index == testString.length() || Character.isDigit(testString.charAt(index))) { if (foundUpperCase && index > startIndex && index - startIndex > longestLength) { longestLength = index - startIndex; endIndex = index; longestStartIndex = startIndex; } startIndex = index + 1; foundUpperCase = false; } else if (Character.isUpperCase(testString.charAt(index))) { foundUpperCase = true; } index++; } System.out.println(testString.substring(longestStartIndex, endIndex));

Java Programming Exercise – Find the longest substrings without any number and at least one upper case character. Posted on July 10, 2017 Input : 1010 Output : 3 Substring 101 has 1 occurring more number of times than 0. Input : 101100 Output : 5 Substring 10110 has 1 occurring more number of times than 0. Recommended : Please try your approach first on IDE and then look at the solution.

You don't need regular expressions. Just use a few integers to act as index pointers into the string:

int i = 0; int longestStart = 0; int longestEnd = 0; while (i < s.length()) { // Skip past all the digits. while (i < s.length() && Character.isDigit(s.charAt(i))) { ++i; } // i now points to the start of a substring // or one past the end of the string. int start = i; // Keep a flag to record if there is an uppercase character. boolean hasUppercase = false; // Increment i until you hit another digit or the end of the string. while (i < s.length() && !Character.isDigit(s.charAt(i))) { hasUppercase |= Character.isUpperCase(s.charAt(i)); ++i; } // Check if this is longer than the longest so far. if (hasUppercase && i - start > longestEnd - longestStart) { longestEnd = i; longestStart = start; } } String longest = s.substring(longestStart, longestEnd);

Whilst more verbose than regular expressions, this has the advantage of not creating any unnecessary objects: the only object created is the longest string, right at the end.

For “GEEKSFORGEEKS”, there are two longest substrings shown in the below by one and check for each substring whether it contains all unique characters or not. window or not, we store last index of every character in an array lasIndex[]. + d) where n is length of the input string and d is number of characters in input Find the longest substring with k unique characters in a given string; Check if sum of Fibonacci elements in an Array is a Fibonacci number or not; Count of subarrays having exactly K perfect square numbers; Check if given words are present in a string; Longest permutation subsequence in a given array; Remove odd frequency characters from the string

C · C++ · Java · Python · SQL · PHP · Javascript · Program Output For example, the longest substrings without repeating characters for If it is after st, then find length of current substring currlen as i-st, where i is current substring without repeating characters is from s[start] to s[start+maxlen-1]. using namespace std;. Find substrings that contain all vowels We have been given a string in lowercase alphabets. We need to print substrings that contain all the vowels at-least one time and there are no consonants (non-vowel characters) present in the substrings.

1. Overview. In this tutorial, compare ways to find the longest substring of unique letters using Java. For example, the longest substring of Previous Next If you want to practice data structure and algorithm programs, you can go through 100+ data structure and algorithm programs. Given two Strings A and B. Find the length of the Longest Common Subsequence (LCS) of the given Strings. Subsequence can contain any number of characters of a string including zero or all (subsequence containing zero characters is called as empty

1. Longest Substring Which Contains 2 Unique Characters. In this solution, a hashmap is charAt(start); int count = map.get(t); if(count>1){ map.put(t, count-1); }else{ LeetCode – Longest Substring with At Least K Repeating Characters (Java) this data structure adds string, allowing only a specified number of duplicates. Given a string s of lowercase letters, we need to find the longest substring length that contain (a, e, i, o, u) only. The idea is to traverse the string and keep track of current number of vowels in the string. If we see a character that is not vowel, we reset count to 0. But before resetting we update max count value which is going to be our

##### Comments

- If you want to do it in an elegant manner, use
`regex`

. Search on google about`Pattern.matches()`

and regex. - Readability level 9999999 (y) :)
- Would you please explain what's happening here?
- But for string
`a0bb`

, it should return -1. - @roger_that Why is that? How can a length be -1?
- @Gabriel: yeah my bad. I was suppose to put an empty check to return -1 as the requirement.
- You should maybe check out the Javadoc for
`java.lang.Character`

. There are a whole bunch of`isSomething`

methods, e.g.`isUpperCase`

, which is much easier and faster to use than your`isCharUpperCase`

method; and`isDigit`

, which covers a broader range of digits than just 0-9. Plus,`if (condition) return true; else return false;`

is the same as`return condition;`

. - Thank you very much @AndyTurner, that boolean if-else structure is the fault of mine that I do every time, now updated :). I always come across with the character problems every time I build a project so wanted to use a general upper case finder for the letter by using the regex literal which I used before, also you are right about that numbers too, I will update that part, thanks again :) Bu you know, the most important thing here is the algorithm in the iteration ;)
- How did you put that "Ideone demo" button?
- Using the
`<kbd></kbd>`

tags. - Thanks for the answer
- Note: you don't need to cast a
`char`

before comparing to`int`

: it is automatically widened. Also, you can use`'0'`

and`'9'`

instead of 48 and 57; it just makes it clear what these magic numbers are. Also`if (condition) return true; return false;`

is the same as`return condition;`

. - @roger_that I solved it for at most 1 uppercase character. I checked the question now. I will modify the code according to that.
- Thanks roger_that. I have modified my code. Kindly let me know, if any more test cases fails.