## hani.amr

BAN USER- 7of 9 votes

AnswersImagine an alphabet of words. Example:

- hani.amr in United States

a ==> 1

b ==> 2

c ==> 3

.

z ==> 26

ab ==> 27

ac ==> 28

.

az ==> 51

bc ==> 52

and so on.

Such that the sequence of characters need to be in ascending order only (ab is valid but ba is not). Given any word print its index if valid and 0 if not.

Input Output

ab 27

ba 0

aez 441

Note: Brute-force is not allowed.| Report Duplicate | Flag | PURGE

Microsoft Software Engineer / Developer Algorithm

yes but I think it could be done more optimized using combinations but couldn't get far more than 3 letters. Formula for 3 letters could look like that:

26C1 + 26C2 + [Σ(26-i) {i=2 to n2-1}] + n3 - n2

Where n1,n2 and n3 represents the index of the character in the alphabet (1->26)

So in the case of aez:

n1 = 1

n2 = 5

n3 = 26

= 26 + 325 + 24 + 23 + 22 + 26 - 5

= 441

So in case I'm in the right track, we need this formula to be more general for n characters where 2 <= n <= 26

**CareerCup**is the world's biggest and best source for software engineering interview preparation. See all our resources.

Open Chat in New Window

BFS/DFS will do, but I prefer doing it with a dynamic connectivity approach as union-find.

- hani.amr September 15, 2014