zoukankan      html  css  js  c++  java
  • Valid Password

    Problem

    In 1-9 keypad one key is not working. If someone enters a password then not working key will not be entered. You have given expected password and entered password. Check that entered password is valid or not. Ex: entered 164, expected 18684 (you need to take care as when u enter18684 and 164 only both will be taken as 164 input)

    Solution


    有几种情况需要考虑下, 

    1. 没有按到过坏的键

    2. 1864, 1684.  坏的是8, 坏的键又出现一次

    2. expect有可能是186848888,要继续检查直到长度相等

     1 public static boolean validPassword(String expect, String actual) {
     2     char faultKey = ''; //initial fault key
     3     int i=0, j=0;    //two pointer for each string
     4     for(; i<actual.length() && j<expect.length(); j++) {
     5         if(actual.charAt(i) != expect.charAt(j)) {
     6             if(faultKey == '')    faultKey = expect.charAt(j);    //record fault key
     7             else if(faultKey != expect.charAt(j))    return false;    //found second fault key
     8             i--;
     9         }
    10         i++;
    11     }
    12     
    13     for(int k=0; k<expect.length() ;k++) {
    14         if(faultKey == expect.charAt(k)) return false;            
    15     }
    16     //fault key may require press many times after that
    17     while(j<expect.length() && expect.charAt(j)==faultKey)    j++;
    18     
    19     return (i==actual.length() && j==expect.length() && faultKey == '') ? true : false;
    20 }
  • 相关阅读:
    Python find()方法
    Python expandtabs()方法
    RGB-D对红外热像仪和毫米波雷达标定
    ADAS虚拟车道边界生成
    3D惯导Lidar SLAM
    语义分割改进:通过视频传播和标签松弛
    YOLOv4:目标检测(windows和Linux下Darknet 版本)实施
    tensorflow-yolov4实施方法
    3D惯导Lidar仿真
    YOLOv4实用训练实践
  • 原文地址:https://www.cnblogs.com/superbo/p/4111923.html
Copyright © 2011-2022 走看看