Your browser (Unknown 0) is no longer supported. Some parts of the website may not work correctly. Please update your browser.

AVAILABLE LESSONS:

Lesson 1

Iterations

Lesson 2

Arrays

Lesson 3

Time Complexity

Lesson 4

Counting Elements

Lesson 5

Prefix Sums

Lesson 6

Sorting

Lesson 7

Stacks and Queues

Lesson 8

Leader

Lesson 9

Maximum slice problem

Lesson 10

Prime and composite numbers

Lesson 11

Sieve of Eratosthenes

Lesson 12

Euclidean algorithm

Lesson 13

Fibonacci numbers

Lesson 14

Binary search algorithm

Lesson 15

Caterpillar method

Lesson 16

Greedy algorithms

Lesson 17

Dynamic programming

Lesson 90

Tasks from Indeed Prime 2015 challenge

Lesson 91

Tasks from Indeed Prime 2016 challenge

Lesson 92

Tasks from Indeed Prime 2016 College Coders challenge

Lesson 99

Future training

Given a string containing words, find the longest word that satisfies specific conditions.

Programming language:
Spoken language:

You would like to set a password for a bank account. However, there are three restrictions on the format of the password:

- it has to contain only alphanumerical characters (
a−z,A−Z,0−9);- there should be an even number of letters;
- there should be an odd number of digits.

You are given a string S consisting of N characters. String S can be divided into *words* by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.

For example, given "`test 5 a0A pass007 ?xy1`", there are five words and three of them are valid passwords: "`5`", "`a0A`" and "`pass007`". Thus the longest password is "`pass007`" and its length is 7. Note that neither "`test`" nor "`?xy1`" is a valid password, because "`?`" is not an alphanumerical character and "`test`" contains an even number of digits (zero).

Write a function:

int solution(char *S);

that, given a non-empty string S consisting of N characters, returns the length of the longest word from the string that is a valid password. If there is no such word, your function should return −1.

For example, given S = "`test 5 a0A pass007 ?xy1`", your function should return 7, as explained above.

Assume that:

- N is an integer within the range [1..200];
- string S consists only of printable ASCII characters and spaces.

In your solution, focus on ** correctness**. The performance of your solution will not be the focus of the assessment.

Copyright 2009–2020 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

You would like to set a password for a bank account. However, there are three restrictions on the format of the password:

- it has to contain only alphanumerical characters (
a−z,A−Z,0−9);- there should be an even number of letters;
- there should be an odd number of digits.

You are given a string S consisting of N characters. String S can be divided into *words* by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.

For example, given "`test 5 a0A pass007 ?xy1`", there are five words and three of them are valid passwords: "`5`", "`a0A`" and "`pass007`". Thus the longest password is "`pass007`" and its length is 7. Note that neither "`test`" nor "`?xy1`" is a valid password, because "`?`" is not an alphanumerical character and "`test`" contains an even number of digits (zero).

Write a function:

int solution(string &S);

that, given a non-empty string S consisting of N characters, returns the length of the longest word from the string that is a valid password. If there is no such word, your function should return −1.

For example, given S = "`test 5 a0A pass007 ?xy1`", your function should return 7, as explained above.

Assume that:

- N is an integer within the range [1..200];
- string S consists only of printable ASCII characters and spaces.

In your solution, focus on ** correctness**. The performance of your solution will not be the focus of the assessment.

Copyright 2009–2020 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

You would like to set a password for a bank account. However, there are three restrictions on the format of the password:

- it has to contain only alphanumerical characters (
a−z,A−Z,0−9);- there should be an even number of letters;
- there should be an odd number of digits.

You are given a string S consisting of N characters. String S can be divided into *words* by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.

For example, given "`test 5 a0A pass007 ?xy1`", there are five words and three of them are valid passwords: "`5`", "`a0A`" and "`pass007`". Thus the longest password is "`pass007`" and its length is 7. Note that neither "`test`" nor "`?xy1`" is a valid password, because "`?`" is not an alphanumerical character and "`test`" contains an even number of digits (zero).

Write a function:

class Solution { public int solution(string S); }

that, given a non-empty string S consisting of N characters, returns the length of the longest word from the string that is a valid password. If there is no such word, your function should return −1.

For example, given S = "`test 5 a0A pass007 ?xy1`", your function should return 7, as explained above.

Assume that:

- N is an integer within the range [1..200];
- string S consists only of printable ASCII characters and spaces.

In your solution, focus on ** correctness**. The performance of your solution will not be the focus of the assessment.

Copyright 2009–2020 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

- it has to contain only alphanumerical characters (
a−z,A−Z,0−9);- there should be an even number of letters;
- there should be an odd number of digits.

*words* by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.

`test 5 a0A pass007 ?xy1`", there are five words and three of them are valid passwords: "`5`", "`a0A`" and "`pass007`". Thus the longest password is "`pass007`" and its length is 7. Note that neither "`test`" nor "`?xy1`" is a valid password, because "`?`" is not an alphanumerical character and "`test`" contains an even number of digits (zero).

Write a function:

func Solution(S string) int

`test 5 a0A pass007 ?xy1`", your function should return 7, as explained above.

Assume that:

- N is an integer within the range [1..200];
- string S consists only of printable ASCII characters and spaces.

** correctness**. The performance of your solution will not be the focus of the assessment.

- it has to contain only alphanumerical characters (
a−z,A−Z,0−9);- there should be an even number of letters;
- there should be an odd number of digits.

*words* by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.

`test 5 a0A pass007 ?xy1`", there are five words and three of them are valid passwords: "`5`", "`a0A`" and "`pass007`". Thus the longest password is "`pass007`" and its length is 7. Note that neither "`test`" nor "`?xy1`" is a valid password, because "`?`" is not an alphanumerical character and "`test`" contains an even number of digits (zero).

Write a function:

class Solution { public int solution(String S); }

`test 5 a0A pass007 ?xy1`", your function should return 7, as explained above.

Assume that:

- N is an integer within the range [1..200];
- string S consists only of printable ASCII characters and spaces.

** correctness**. The performance of your solution will not be the focus of the assessment.

- it has to contain only alphanumerical characters (
a−z,A−Z,0−9);- there should be an even number of letters;
- there should be an odd number of digits.

*words* by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.

`test 5 a0A pass007 ?xy1`", there are five words and three of them are valid passwords: "`5`", "`a0A`" and "`pass007`". Thus the longest password is "`pass007`" and its length is 7. Note that neither "`test`" nor "`?xy1`" is a valid password, because "`?`" is not an alphanumerical character and "`test`" contains an even number of digits (zero).

Write a function:

class Solution { public int solution(String S); }

`test 5 a0A pass007 ?xy1`", your function should return 7, as explained above.

Assume that:

- N is an integer within the range [1..200];
- string S consists only of printable ASCII characters and spaces.

** correctness**. The performance of your solution will not be the focus of the assessment.

- it has to contain only alphanumerical characters (
a−z,A−Z,0−9);- there should be an even number of letters;
- there should be an odd number of digits.

*words* by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.

`test 5 a0A pass007 ?xy1`", there are five words and three of them are valid passwords: "`5`", "`a0A`" and "`pass007`". Thus the longest password is "`pass007`" and its length is 7. Note that neither "`test`" nor "`?xy1`" is a valid password, because "`?`" is not an alphanumerical character and "`test`" contains an even number of digits (zero).

Write a function:

function solution(S);

`test 5 a0A pass007 ?xy1`", your function should return 7, as explained above.

Assume that:

- N is an integer within the range [1..200];
- string S consists only of printable ASCII characters and spaces.

** correctness**. The performance of your solution will not be the focus of the assessment.

- it has to contain only alphanumerical characters (
a−z,A−Z,0−9);- there should be an even number of letters;
- there should be an odd number of digits.

*words* by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.

`test 5 a0A pass007 ?xy1`", there are five words and three of them are valid passwords: "`5`", "`a0A`" and "`pass007`". Thus the longest password is "`pass007`" and its length is 7. Note that neither "`test`" nor "`?xy1`" is a valid password, because "`?`" is not an alphanumerical character and "`test`" contains an even number of digits (zero).

Write a function:

fun solution(S: String): Int

`test 5 a0A pass007 ?xy1`", your function should return 7, as explained above.

Assume that:

- N is an integer within the range [1..200];
- string S consists only of printable ASCII characters and spaces.

** correctness**. The performance of your solution will not be the focus of the assessment.

- it has to contain only alphanumerical characters (
a−z,A−Z,0−9);- there should be an even number of letters;
- there should be an odd number of digits.

*words* by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.

`test 5 a0A pass007 ?xy1`", there are five words and three of them are valid passwords: "`5`", "`a0A`" and "`pass007`". Thus the longest password is "`pass007`" and its length is 7. Note that neither "`test`" nor "`?xy1`" is a valid password, because "`?`" is not an alphanumerical character and "`test`" contains an even number of digits (zero).

Write a function:

function solution(S)

`test 5 a0A pass007 ?xy1`", your function should return 7, as explained above.

Assume that:

- N is an integer within the range [1..200];
- string S consists only of printable ASCII characters and spaces.

** correctness**. The performance of your solution will not be the focus of the assessment.

- it has to contain only alphanumerical characters (
a−z,A−Z,0−9);- there should be an even number of letters;
- there should be an odd number of digits.

*words* by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.

`test 5 a0A pass007 ?xy1`", there are five words and three of them are valid passwords: "`5`", "`a0A`" and "`pass007`". Thus the longest password is "`pass007`" and its length is 7. Note that neither "`test`" nor "`?xy1`" is a valid password, because "`?`" is not an alphanumerical character and "`test`" contains an even number of digits (zero).

Write a function:

int solution(NSString *S);

`test 5 a0A pass007 ?xy1`", your function should return 7, as explained above.

Assume that:

- N is an integer within the range [1..200];
- string S consists only of printable ASCII characters and spaces.

** correctness**. The performance of your solution will not be the focus of the assessment.

- it has to contain only alphanumerical characters (
a−z,A−Z,0−9);- there should be an even number of letters;
- there should be an odd number of digits.

*words* by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.

`test 5 a0A pass007 ?xy1`", there are five words and three of them are valid passwords: "`5`", "`a0A`" and "`pass007`". Thus the longest password is "`pass007`" and its length is 7. Note that neither "`test`" nor "`?xy1`" is a valid password, because "`?`" is not an alphanumerical character and "`test`" contains an even number of digits (zero).

Write a function:

function solution(S: PChar): longint;

`test 5 a0A pass007 ?xy1`", your function should return 7, as explained above.

Assume that:

- N is an integer within the range [1..200];
- string S consists only of printable ASCII characters and spaces.

** correctness**. The performance of your solution will not be the focus of the assessment.

- it has to contain only alphanumerical characters (
a−z,A−Z,0−9);- there should be an even number of letters;
- there should be an odd number of digits.

*words* by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.

`test 5 a0A pass007 ?xy1`", there are five words and three of them are valid passwords: "`5`", "`a0A`" and "`pass007`". Thus the longest password is "`pass007`" and its length is 7. Note that neither "`test`" nor "`?xy1`" is a valid password, because "`?`" is not an alphanumerical character and "`test`" contains an even number of digits (zero).

Write a function:

function solution($S);

`test 5 a0A pass007 ?xy1`", your function should return 7, as explained above.

Assume that:

- N is an integer within the range [1..200];
- string S consists only of printable ASCII characters and spaces.

** correctness**. The performance of your solution will not be the focus of the assessment.

- it has to contain only alphanumerical characters (
a−z,A−Z,0−9);- there should be an even number of letters;
- there should be an odd number of digits.

*words* by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.

`test 5 a0A pass007 ?xy1`", there are five words and three of them are valid passwords: "`5`", "`a0A`" and "`pass007`". Thus the longest password is "`pass007`" and its length is 7. Note that neither "`test`" nor "`?xy1`" is a valid password, because "`?`" is not an alphanumerical character and "`test`" contains an even number of digits (zero).

Write a function:

sub solution { my ($S)=@_; ... }

`test 5 a0A pass007 ?xy1`", your function should return 7, as explained above.

Assume that:

- N is an integer within the range [1..200];
- string S consists only of printable ASCII characters and spaces.

** correctness**. The performance of your solution will not be the focus of the assessment.

- it has to contain only alphanumerical characters (
a−z,A−Z,0−9);- there should be an even number of letters;
- there should be an odd number of digits.

*words* by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.

`test 5 a0A pass007 ?xy1`", there are five words and three of them are valid passwords: "`5`", "`a0A`" and "`pass007`". Thus the longest password is "`pass007`" and its length is 7. Note that neither "`test`" nor "`?xy1`" is a valid password, because "`?`" is not an alphanumerical character and "`test`" contains an even number of digits (zero).

Write a function:

def solution(S)

`test 5 a0A pass007 ?xy1`", your function should return 7, as explained above.

Assume that:

- N is an integer within the range [1..200];
- string S consists only of printable ASCII characters and spaces.

** correctness**. The performance of your solution will not be the focus of the assessment.

- it has to contain only alphanumerical characters (
a−z,A−Z,0−9);- there should be an even number of letters;
- there should be an odd number of digits.

*words* by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.

`test 5 a0A pass007 ?xy1`", there are five words and three of them are valid passwords: "`5`", "`a0A`" and "`pass007`". Thus the longest password is "`pass007`" and its length is 7. Note that neither "`test`" nor "`?xy1`" is a valid password, because "`?`" is not an alphanumerical character and "`test`" contains an even number of digits (zero).

Write a function:

def solution(s)

`test 5 a0A pass007 ?xy1`", your function should return 7, as explained above.

Assume that:

- N is an integer within the range [1..200];
- string S consists only of printable ASCII characters and spaces.

** correctness**. The performance of your solution will not be the focus of the assessment.

- it has to contain only alphanumerical characters (
a−z,A−Z,0−9);- there should be an even number of letters;
- there should be an odd number of digits.

*words* by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.

`test 5 a0A pass007 ?xy1`", there are five words and three of them are valid passwords: "`5`", "`a0A`" and "`pass007`". Thus the longest password is "`pass007`" and its length is 7. Note that neither "`test`" nor "`?xy1`" is a valid password, because "`?`" is not an alphanumerical character and "`test`" contains an even number of digits (zero).

Write a function:

object Solution { def solution(s: String): Int }

`test 5 a0A pass007 ?xy1`", your function should return 7, as explained above.

Assume that:

- N is an integer within the range [1..200];
- string S consists only of printable ASCII characters and spaces.

** correctness**. The performance of your solution will not be the focus of the assessment.

- it has to contain only alphanumerical characters (
a−z,A−Z,0−9);- there should be an even number of letters;
- there should be an odd number of digits.

*words* by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.

`test 5 a0A pass007 ?xy1`", there are five words and three of them are valid passwords: "`5`", "`a0A`" and "`pass007`". Thus the longest password is "`pass007`" and its length is 7. Note that neither "`test`" nor "`?xy1`" is a valid password, because "`?`" is not an alphanumerical character and "`test`" contains an even number of digits (zero).

Write a function:

public func solution(_ S : inout String) -> Int

`test 5 a0A pass007 ?xy1`", your function should return 7, as explained above.

Assume that:

- N is an integer within the range [1..200];
- string S consists only of printable ASCII characters and spaces.

** correctness**. The performance of your solution will not be the focus of the assessment.

- it has to contain only alphanumerical characters (
a−z,A−Z,0−9);- there should be an even number of letters;
- there should be an odd number of digits.

*words* by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.

`test 5 a0A pass007 ?xy1`", there are five words and three of them are valid passwords: "`5`", "`a0A`" and "`pass007`". Thus the longest password is "`pass007`" and its length is 7. Note that neither "`test`" nor "`?xy1`" is a valid password, because "`?`" is not an alphanumerical character and "`test`" contains an even number of digits (zero).

Write a function:

Private Function solution(S As String) As Integer

`test 5 a0A pass007 ?xy1`", your function should return 7, as explained above.

Assume that:

- N is an integer within the range [1..200];
- string S consists only of printable ASCII characters and spaces.

** correctness**. The performance of your solution will not be the focus of the assessment.

Information about upcoming challenges, solutions and lessons directly in your inbox.

© 2009–2020 Codility Ltd., registered in England and Wales (No. 7048726). VAT ID GB981191408. Registered office: 107 Cheapside, London EC2V 6DN