Alphabet Subsequence

What is Subsequence? A subsequence of   is a sequence   defined by  , where   is an increasing sequence of indices . For example, the prime ...

What is Subsequence?

A subsequence of {a} is a sequence {b} defined by b_k=a_(n_k), where n_1<n_2<... is an increasing sequence of indices .

For example, the prime numbers are a subsequence of the positive integers.

ASCII of 'a' is 97, 'b' is 98, 'c' is 98 and so on. If the string is 'abc' then it is subsequence because a<b<c. Similarly if string is 'azb' then it is not subsequence because a<z>b.

 

Implementation

Method 1

function alphabetSubsequence(str) {

    //  write code here.

    flag = 0;

    for(let i=0; i<str.length-1;i++){

        a1cc = str.charCodeAt(i);

        a2cc = str.charCodeAt(i+1);

        if(a1cc<a2cc){

            flag = 1;

        }

        else{

            flag = 0;

            break;

        }

    }

    if(flag === 1){

        return true;

    }

    else{

        return false;

    }

}


Method 2

function alphabetSubsequence(str) {

    const chars = str.split('');

    const charCodes = chars.map((char) => char.charCodeAt(0));    

    if(new Set(charCodes).size !== charCodes.length) {

        return false;

    }

    for (let i = 0; i < charCodes.length - 1; i++) {

        if(charCodes[i] > charCodes[i + 1]) {

            return false;

        }

    }    

    return true;

}



/**

* Test Suite 

*/

describe('alphabetSubsequence()', () => {

    it('returns false when it has duplicate letters', () => {

        // arrange

        const str = 'effg';        

        // act

        const result = alphabetSubsequence(str);

        // log

        console.log("result 1: ", result);        

        // assert

        expect(result).toBe(false);

    });


    it('returns false when NOT in ascending a - z order', () => {

        // arrange

        const str = 'cdce';        

        // act

        const result = alphabetSubsequence(str);

        // log

        console.log("result 2: ", result);        

        // assert

        expect(result).toBe(false);

    });    

    it('returns true whenno duplicates and is ascending a - z order ', () => {

        // arrange

        const str = 'ace';        

        // act

        const result = alphabetSubsequence(str);

        // log

        console.log("result 3: ", result);        

        // assert

        expect(result).toBe(true);

    });

});

COMMENTS

Name

Accident Alert,1,AI,2,Array,1,Aurdino,1,C,2,Computer Graphics,9,Data Science,3,Dataset,1,Decoratot,1,Django,1,ESP32,1,Fixed point/iteration method,1,Greater or smaller,1,html,1,Image Processing,1,JAVA,1,Javascript,22,Machine Learning,1,Matlab,3,Numerical Method,13,OOP,1,Other,3,PHP,1,Point operation,1,Python,11,Raspberry pi,1,Recommendation System,1,Regression,1,Reservation System,1,Robotics,1,Simulation,2,sine wave,1,String Handling Function,1,Web scrap,1,Webpage,1,
ltr
item
COMPUTER PROGRAMMING: Alphabet Subsequence
Alphabet Subsequence
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikiSyW1oeaxBKWfD1N5EOZAfirySmEoyF-GENVp5IgPfILa9CwRq-F2eVyuSevuQGfkBhxX4Rlg5MMIYkHLtq57GPVRVcde4XxB28g1Y5mjq5SegnP5snonv4C-Cq4FtIzZaPEen0c8i7D/w640-h320/Screenshot+from+2020-12-19+18-30-05.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikiSyW1oeaxBKWfD1N5EOZAfirySmEoyF-GENVp5IgPfILa9CwRq-F2eVyuSevuQGfkBhxX4Rlg5MMIYkHLtq57GPVRVcde4XxB28g1Y5mjq5SegnP5snonv4C-Cq4FtIzZaPEen0c8i7D/s72-w640-c-h320/Screenshot+from+2020-12-19+18-30-05.png
COMPUTER PROGRAMMING
https://computerprogram4ru.blogspot.com/2020/12/alphabet-subsequence.html
https://computerprogram4ru.blogspot.com/
https://computerprogram4ru.blogspot.com/
https://computerprogram4ru.blogspot.com/2020/12/alphabet-subsequence.html
true
8672391763020279633
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy