s = 'woimoepzbjvxfafpyfpzgmxugjodtemcjcpoxobfgbsmokkmcdpmawcwwaxhqwabzdlplvteszqgtkamxjkswkpnzpxpudxcmigz'
The given variable 's' is a sequence of random alphabets.
What is length of the longest sub-string of 's' in which the letters occur in alphabetical order.
For example, if s = 'azcbobobegghakl', then the longest sub-string in which letters occur in alphabetical order is 'beggh' with length 5.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Lucky that the length of s is less than 99 and there is no adjacent same letters in s that give the length of possible solution more than 4.
thanks in random friendship to brilliant.
Here is a solution in C
main()
{
int i, x=1, bigger=0, p1, p2;
char s[] =
"woimoepzbjvxfafpyfpzgmxugjodtemcjcpoxobfgbsmokkmcdpmawcwwaxhqwabzdlplvteszqgtkamxjkswkpnzpxpudxcmigz";
for(i=0; i<100; i++)
{
p1=s[i];
p2=s[i+1];
if( p1 > p2 )
x=1;
else x++;
if(x>bigger)
{
bigger=x;
}
} printf("%d\n", bigger); }
Cool, though I don't understand how this works: I know it works, I tested it, I only changed the name of variable "max" because it is a built-in symbol in Python 3.3.3, would you mind explaining? I didn't know Python could compare elements of a string like that... I normally use C# or C++.
Log in to reply
It's comparing char element with their ASCII value. 'a' = 97 and 'b' = 98 if you compare 'a' < 'b', it will return true. same in C++ // ord() function in python return ASCII value of char too; ord('a') must return 97
Yes, max is a built in function in both python 2 and 3. You probably shouldn't use a variable with that name.
Hmm; I didn't either. I think it's implicitly calling the ord() function on the strings.
I didn't know you can simply compare characters without conversion to an integer first, thanks... Now I've learnt something new
Here is a solution in C#. FindMax is a simple iterative function.
int cn = 1;
ArrayList ar = new ArrayList();
string s = "woimoepzbjvxfafpyfpzgmxugjodtemcjcpoxobfgbsmokkm
cdpmawcwwaxhqwabzdlplvteszqgtkamxjkswkpnzpxpudxcmigz";
byte[] asciiBytes = Encoding.ASCII.GetBytes(s);
for (int i = 0; i < asciiBytes.Length; i++)
{
try
{
if (int.Parse(asciiBytes[i].ToString()) <= int.Parse(asciiBytes[i + 1].ToString()))
{
cn += 1;
}
else
{
ar.Add(cn);
cn = 1;
}
}
catch
{
}
}
MessageBox.Show(FindMax(ar).ToString());
public static void main(String[] args) {
String str="woimoepzbjvxfafpyfpzgmxugjodtemcjcpoxobfgbsmokkmcdpmawcwwaxhqwabzdlplvteszqgtkamxjkswkpnzpxpudxcmigz";
int maxSubStrLength=1;int max=1;
for(int i=0;i<str.length()-1;i++)
{
char a,b;
a=str.charAt(i);
if(i==str.length())
break;
b=str.charAt(i+1);
if(a<b)
{
maxSubStrLength++;
}
else
{
if(maxSubStrLength>=max)
max=maxSubStrLength;
maxSubStrLength=1;
}
}
System.out.println(max);
}
I think it will be better if you work with ASCII value. Python code : ```python s='woimoepzbjvxfafpyfpzgmxugjodtemcjcpoxobfgbsmokkmcdpmawcwwaxhqwabzdlplvteszqgtkamxjkswkpnzpxpudxcmigz'
count , max = 1,1
for i in range(len(s)-1): if ord(s[i+1])<ord(s[i]): count = 1 if ord(s[i+1])> ord(s[i]): count +=1 if count > max: max = count print(max) ```
public class sequ
{
void main()
{ String x="woimoepzbjvxfafpyfpzgmxugjodtemcjcpoxobfgbsmokkmcdpmawcwwaxhqwabzdlplvteszqgtkamxjkswkpnzpxpudxcmigz" ;
int max=0;
for(int i=0;i<x.length();i++)
{
String y="";
for(int j=i;j<x.length();j++)
{
char ch = x.charAt(j);
y=y+ch;
String z="";
for(char k='a';k<='z';k++)
{
for(int l=0;l<y.length();l++)
{
char c = y.charAt(l);
if(c==k)
z=z+c;
}
if(y.equalsIgnoreCase(z))
{
if(y.length()>max)
max=y.length();
}
}
}
}
System.out.println(max);
}
}
c++ solution
using namespace std;
int main() {
string s ; s="woimoepzbjvxfafpyfpzgmxugjodtemcjcpoxobfgbsmokkmcdpmawcwwaxhqwabzdlplvteszqgtkamxjkswkpnzpxpudxcmigz";
int len=s.size();
int count=1,i;
int max=-10000;
for(i=0;i < len;i++)
{
if(s[i]<s[i+1])
{
count++;
}
else {
count=1;
}
if(count > max)
{
max=count;
}
}
cout << max;
return 0;
}
It's pretty trivial to do this in Javascript. Essentially, I converted each character to its corresponding number (ie. a → 1, b → 2, c → 3, ... , z → 26) and then found the length of the longest subsequence for which those numbers were increasing:
var string = 'woimoepzbjvxfafpyfpzgmxugjodtemcjcpoxobfgbsmokkmcdpmawcwwaxhqwabzdlplvteszqgtkamxjkswkpnzpxpudxcmigz'.toUpperCase();
var characterNumbers = [];
var increasingLengths = [];
for (var i = 0; i < string.length; i++) {
// Find the corresponding number for this character
characterNumbers[i] = string.charCodeAt(i) - 64;
if (characterNumbers[i] > characterNumbers[i - 1] && i > 0) {
// If this character is alphabetically "greater" than the one before it, add to the length of the current sequence
// Also, if we're just starting out a sequence, mark this number and the one before it
increasingLengths[increasingLengths.length - 1] += (increasingLengths[increasingLengths.length - 1] == 0) ? 2 : 1
} else if (increasingLengths[increasingLengths.length - 1] != 0) {
// Otherwise, if a sequence just ended, create a new sequence
increasingLengths.push(0);
}
}
// Log the length of the longest sequence of characters in alphabetical order
console.log(Math.max.apply(Math, increasingLengths));
Waaw!!
Java Solution :::
import java.io.*;
public class StringSting
{
public static void main(String args[])
{
String
s="woimoepzbjvxfafpyfpzgmxugjodtemcjcpoxobfgbsmokkmcdpmawcwwaxhqwabzdlplvteszqgtkamxjkswkpnzpxpudxcmi gz";
char c='w';
int i=0,j=s.length(),k=0,l=0;
for(i=1;i<j;i++)
{
c=s.charAt(i);
if(c>s.charAt(i-1))
{
k++;
}
else
{
if(k>l)
l=k;
k=0;
}
}
System.out.println(l+1);
}
}
OUTPUT :: 4
Python solution:
s = 'woimoepzbjvxfafpyfpzgmxugjodtemcjcpoxobfgbsmokkmcdpmawcwwaxhqwabzdlplvteszqgtkamxjkswkpnzpxpudxcmigz'
ans = []
past = 'z'
x = 1
for index in xrange(len(s)):
if index:
past = current
current = s[index]
if ord(current) >= ord(past):
x += 1
else:
ans.append(x)
x = 1
Here is a solution in C language with logic explained: /* Take three variables : strLength: length of current found sub-string endIndex: array index of the sub-string with maximum length previousLength: length of sub-string with maximum length found last time **/
int strLength = 1;
int endIndex = 0;
int previousLength = 0;
char *randomString = (char *)"woimoepzbjvxfafpyfpzgmxugjodtemcjcpoxobfgbsmokkmcdpmawcwwaxhqwabzdlplvteszqgtkamxjkswkpnzpxpudxcmigz";
/*
Finds the length of longest sub-string with letters in alphabetical order and
stores the end index of sub-string with maximum length and length of the sub-string.
**/
for (int i = 0; i < strlen(randomString); i++)
{
if (randomString[i] <= randomString[i+1])
{
strLength += 1;
}
else
{
if (strLength > previousLength)
{
endIndex = i+1;
previousLength = strLength;
}
strLength = 1;
}
}
//prints the found sub-string and its length.
printf("Length of sub string is %d\n", previousLength);
for (int i = endIndex - previousLength; i < endIndex; i++)
{
printf("%c",randomString[i]);
}
Solution printed:
Length of sub string is 4.
sub-string is: bjvx
This is the C program I wrote:
char s[] = "woi........migz";
int index1, index2, count = 0, count2, temp, result = 0;
index1 = 0;
temp = 'a';
while(s[index1] != '\0'){
for(index2 = temp; index2 <= 'z'; index2++) {
count2 = count;
if(s[index1] == index2) {
count++;
temp = index2;
break;
}
}
if(count == count2) {
if(count > result) {
result = count;
}
count = 0;
temp = 'a';
}
else {
index1++;
}
}
printf("%d", result);
1 2 3 4 5 6 7 8 9 10 11 |
|
Use your eyes haha
In Python (with explanation)
def checkString( s ):
#to put running streak
idx1 = 1
#to put max running streak
idx2 = 1
#start loop from second character
for aLoop in range(1,len(s)):
if s[aaa] >= s[aaa-1]:
#same character or in alphabetical order, add the streak
idx1 += 1
else:
#this streak ends here
if idx1 > idx2:
#save if this is the longest streak
idx2 = idx1
#reset the running streak
idx1 = 1
#show the result
print idx2
#put any string to check here
s= 'woimoepzbjvxfafpyfpzgmxugjodtemcjcpoxobfgbsmokkmcdpmawcwwaxhqwabzdlplvteszqgtkamxjkswkpnzpxpudxcmigz'
checkString(s)
C Code
main() {
int i, x=1, bigger=0, p1, p2;
char s[] = "woimoepzbjvxfafpyfpzgmxugjodtemcjcpoxobfgbsmokkmcdpmawcwwaxhqwabzdlplvteszqgtkamxjkswkpnzpxpudxcmigz";
for(i=0; i<100; i++)
{
p1=s[i];
p2=s[i+1];
if( p1 > p2 )
x=1;
else x++;
if(x>bigger)
{
bigger=x;
}
}
printf("%d\n", bigger);
#include<stdio.h>
using namespace std;
int main()
{
char A[200];
gets(A);
char last=A[0];
int length=1,maxlength=1;
for(int i=1;A[i]!='\0';i++)
{
if(A[i]>=last)
{
length++;
if(length>maxlength)
maxlength=length;
last=A[i];
}
else
{
last=A[i];
length=1;
}
}
cout<<maxlength;
}
I just saw the string ...wrote it on the paper and found out all the strings which are in alphabetical order in string s ..... :P
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
getline(cin, str);
int longest=0;
int length=0;
string maxword, word;
char x;
for(int i=0; i<str.length(); i++)
{
x=str[i+1];
if(str[i]<=x)
{
length++;
word+=str[i];
}
else
{
word+=str[i];
if(length>longest)
{
longest=length;
maxword=word;
}
length=0;
word="";
}
}
cout << maxword;
return 0;
}
I was able to find the solution by writing the following python code:
s = '....'
n, i, p = 1, 1, 0
#n stores the current highest sequence
#i is the counter
#p is the ascii value of the previous character
for c in s:
if ord(c) >= p:
i += 1 #if c follows p, increase the counter
n = 1 if i > n else n
else:
i = 1 #reset the counter to one if it doesn't follow p
p = ord(c) #set p to the ascii value of c
print n
This is a solution using the borland c++ compiler turboc 3.0,though this should run on other compilers by just changing the headers
#include<iostream.h>
#include<conio.h>
#include<string.h>
char
s[100]="woimoepzbjvxfafpyfpzgmxugjodtemcjcpoxobfgbsmokkmcdpmawcwwaxhqwabzdlplvteszqgtkamxjkswkpnzpxpudxcmigz";
int i,j;
void main()
{
clrscr();
cout<<"s="<<s<<endl<<endl;
int k,c=0,l; //c stores the length of the longest substring in alphabetical order //l stores the no of characters which are in alphabetical order in each iteration
k=strlen(s);
for(i=0;i<k;i++)
{
l=1;
for(j=i+1;j<k;j++)
{
if(s[j]>=s[j-1])
{
l++;
}
else break;
}
if(l>=c)
{
c=l;
}
}
cout<<"The highest substring of s in alphabetical order is"<<c<<"\n";
getch();
}
Problem Loading...
Note Loading...
Set Loading...
Python code